Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Vitruveo/vtru-studio-api into main
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos committed Nov 28, 2024
2 parents a0d0f18 + c5eaafe commit bd0b9f5
Show file tree
Hide file tree
Showing 35 changed files with 635 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtru-studio-api",
"version": "1.0.582",
"version": "1.0.601",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/constants/rabbitmq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export const RABBITMQ_ERROR_TIMEOUT_BEFORE_EXIT = process.env
.RABBITMQ_ERROR_TIMEOUT_BEFORE_EXIT
? parseInt(process.env.RABBITMQ_ERROR_TIMEOUT_BEFORE_EXIT, 10)
: 5000;
export const RABBITMQ_EXCHANGE_UPDATE_USERNAME_IN_ASSETS =
process.env.RABBITMQ_EXCHANGE_UPDATE_CREATOR_USERNAME_IN_ASSETS ||
'updateCreatorUsernameInAssets';
10 changes: 10 additions & 0 deletions src/features/assets/controller/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Request, Router } from 'express';

import * as model from '../model';
import * as modelRequestConsign from '../../requestConsign/model';
import * as modelCreator from '../../creators/model';
import { middleware } from '../../users';
import {
APIResponse,
Expand Down Expand Up @@ -273,6 +274,7 @@ route.post('/', validateBodyForCreate, async (req, res) => {
assetMetadata: model.AssetsDocument['assetMetadata'];
licenses: model.AssetsDocument['licenses'];
terms: model.AssetsDocument['terms'];
creator: model.AssetsDocument['creator'];
} | null = null;

if (req.body.cloneId) {
Expand All @@ -287,6 +289,13 @@ route.post('/', validateBodyForCreate, async (req, res) => {
asset.actions.countClone = 0;
}

if (!asset?.creator) {
const creator = await modelCreator.findCreatorById({
id: req.auth.id,
});
asset.creator = { username: creator?.username || '' };
}

asset.actions.countClone += 1;

await model.updateAssets({
Expand All @@ -298,6 +307,7 @@ route.post('/', validateBodyForCreate, async (req, res) => {
assetMetadata: asset?.assetMetadata,
licenses: asset?.licenses,
terms: asset?.terms,
creator: asset?.creator,
};
clone.assetMetadata.context.formData.title += ` ${asset.actions.countClone}`;
clone.licenses.print.added = false;
Expand Down
6 changes: 6 additions & 0 deletions src/features/assets/controller/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
schemaAssetUpdateManyNudity,
} from './schemas';
import { schemaValidationForPatchAssetPrice } from './schemaValidate';
import { model } from '../../creators';

export const validateBodyForCreate = async (
req: Request,
Expand All @@ -47,6 +48,11 @@ export const validateBodyForCreate = async (
req.body.framework = createRecordFramework({
createdBy: req.auth.id,
});

const creator = await model.findCreatorById({ id: req.auth.id });
req.body.creator = {
username: creator?.username || '',
};
next();
} catch (error) {
res.status(400).json({
Expand Down
4 changes: 4 additions & 0 deletions src/features/assets/controller/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export interface Spotlight {
preview: string;
author: string;
nudity: string;
vault: {
transactionhash: string | null;
vaultAddress: string | null;
};
}

export interface ArtistSpotlight {
Expand Down
22 changes: 17 additions & 5 deletions src/features/assets/model/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
UpdateManyAssetSpotlightParams,
FindMyAssetsParams,
CountArtsByCreatorParams,
UpateAssetsUsernameParams,
} from './types';
import { FindOptions, getDb, ObjectId } from '../../../services/mongo';
import { buildFilterColorsQuery } from '../utils/color';
Expand Down Expand Up @@ -219,7 +220,7 @@ export const findAssetGroupPaginated = ({
},
{
$addFields: {
username: '$creator.username',
vault: '$creator.vault',
},
},
{
Expand Down Expand Up @@ -319,23 +320,23 @@ export const findAssetsPaginated = ({
from: 'creators',
localField: 'creatorId',
foreignField: '_id',
as: 'creator',
as: 'creatorDetails',
},
},
{
$unwind: {
path: '$creator',
path: '$creatorDetails',
},
},
{
$addFields: {
username: '$creator.username',
vault: '$creatorDetails.vault',
},
},
{
$project: {
creatorId: 0,
creator: 0,
creatorDetails: 0,
},
},
{ $sort: sort },
Expand Down Expand Up @@ -1037,6 +1038,7 @@ export const findLastSoldAssets = () =>
preview: '$formats.preview.path',
price: '$licenses.nft.single.editionPrice',
username: '$creator.username',
vault: '$creator.vault',
},
},
])
Expand Down Expand Up @@ -1197,6 +1199,7 @@ export const findAssetsForSpotlight = ({
preview: '$asset.formats.preview.path',
username: '$creator.username',
nudity: '$asset.assetMetadata.taxonomy.formData.nudity',
vault: '$creator.vault',
},
},
])
Expand Down Expand Up @@ -1235,3 +1238,12 @@ export const getTotalPrice = async (query = {}) =>

export const countArtsByCreator = async ({ query }: CountArtsByCreatorParams) =>
assets().countDocuments(query);

export const updateAssetsUsername = async ({
data,
username,
}: UpateAssetsUsernameParams) =>
assets().updateMany(
{ _id: { $in: data.map((item) => new ObjectId(item._id)) } },
{ $set: { 'creator.username': username } }
);
3 changes: 3 additions & 0 deletions src/features/assets/model/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ export const AssetsSchema = z.object({
updatedBy: z.string().nullable().default(null),
})
.default({}),
creator: z.object({
username: z.string().default(''),
}),
});

export type Assets = z.infer<typeof AssetsSchema>;
Expand Down
5 changes: 5 additions & 0 deletions src/features/assets/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ export interface FindAssetsForSpotlightParams {
export interface CountArtsByCreatorParams {
query: any;
}

export interface UpateAssetsUsernameParams {
data: AssetsDocument[];
username: string;
}
6 changes: 6 additions & 0 deletions src/features/assets/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export const queryByTitleOrDescOrCreator = ({
},
},
},
{
'creator.username': {
$regex: name,
$options: 'i',
},
},
];
export interface querySortSearchParams {
order: string;
Expand Down
3 changes: 2 additions & 1 deletion src/features/creators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as model from './model';
import * as controller from './controller';
import * as watcher from './watcher';
import * as queue from './queue';

export { model, controller, watcher };
export { model, controller, watcher, queue };
19 changes: 17 additions & 2 deletions src/features/creators/model/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
updateCreatorSearchVideoParams,
UpdateCreatorSocialById,
RemoveCreatorSocialById,
PpdateCreatorSearchGridParams,
UpdateCreatorSearchGridParams,
FindCreatorAssetsByGridId,
FindCreatorAssetsByVideoId,
FindCreatorAssetsBySlideshowId,
Expand All @@ -31,6 +31,7 @@ import type {
ChangeStepsSynapsParams,
SynapsSessionInitParams,
ChangeTruLevelParams,
CheckHashAlreadyExistsParams,
} from './types';
import { getDb, ObjectId } from '../../../services/mongo';

Expand Down Expand Up @@ -213,14 +214,16 @@ export const updateCreatorSocialById = ({
export const updateCreatorSearchGrid = ({
id,
grid,
}: PpdateCreatorSearchGridParams) =>
hash,
}: UpdateCreatorSearchGridParams) =>
creators().updateOne(
{ _id: new ObjectId(id) },
{
$push: {
'search.grid': {
...grid,
createdAt: new Date(),
hash,
},
},
}
Expand Down Expand Up @@ -351,6 +354,7 @@ export const findCreatorsStacks = async ({
$project: {
_id: 1,
username: 1,
vault: 1,
stacks: {
$reduce: {
input: inputReducer,
Expand Down Expand Up @@ -732,3 +736,14 @@ export const changeTruLevel = async ({

return result;
};

export const checkHashAlreadyExists = async ({
hash,
}: CheckHashAlreadyExistsParams) =>
creators()
.aggregate([
{
$match: { 'search.grid.hash': hash },
},
])
.toArray();
6 changes: 5 additions & 1 deletion src/features/creators/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface FindCreatorsByName {
name: string;
}

export interface PpdateCreatorSearchGridParams {
export interface UpdateCreatorSearchGridParams {
id: string;
grid: {
id: string;
Expand All @@ -133,6 +133,7 @@ export interface PpdateCreatorSearchGridParams {
title: string;
description: string;
};
hash: string;
}

export interface UpdateCreatorSearchSlideshowParams {
Expand Down Expand Up @@ -201,3 +202,6 @@ export interface SynapsSessionInitParams {
sessionId: string;
creatorId: string;
}
export interface CheckHashAlreadyExistsParams {
hash: string;
}
14 changes: 14 additions & 0 deletions src/features/creators/queue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { uniqueExecution } from '@nsfilho/unique';
import debug from 'debug';

import { startQueueUpdateUsernameInAssets } from './read';

const logger = debug('features:creators:queue');

uniqueExecution({
name: 'startQueueUpdateUsernameInAssets',
callback: () =>
startQueueUpdateUsernameInAssets().catch((error) => {
logger('Error startQueueUpdateUsernameInAssets', error);
}),
});
Loading

0 comments on commit bd0b9f5

Please sign in to comment.