Skip to content

Commit

Permalink
feat: add route to upload template
Browse files Browse the repository at this point in the history
  • Loading branch information
m-paice committed Dec 16, 2024
1 parent 2cdf614 commit 4eafa24
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 3 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.609",
"version": "1.0.610",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/constants/rabbitmq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const RABBITMQ_EXCHANGE_GRID =
process.env.RABBITMQ_EXCHANGE_GRID || 'grid';
export const RABBITMQ_EXCHANGE_CREATORS =
process.env.RABBITMQ_EXCHANGE_CREATORS || 'creators';
export const RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES =
process.env.RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES || 'artcardsTemplates';
export const RABBITMQ_EXCHANGE_RSS = process.env.RABBITMQ_EXCHANGE_RSS || 'rss';
export const RABBITMQ_EXCHANGE_AUTO_CONSIGN =
process.env.RABBITMQ_EXCHANGE_AUTO_CONSIGN || 'autoConsign';
Expand Down
1 change: 1 addition & 0 deletions src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * as events from './events';
export * as schedules from './schedules';
export * as dashboard from './dashboard';
export * as stores from './stores';
export * as templates from './templates';
66 changes: 66 additions & 0 deletions src/features/templates/controller/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import debug from 'debug';
import { nanoid } from 'nanoid';
import { Router } from 'express';

import { sendToExchangeTemplate } from '../../../services/upload';
import { APIResponse } from '../../../services/express';

const logger = debug('features:templates:controller');
const route = Router();

route.post('/image/upload', async (req, res) => {
const transactionApiId = nanoid();

try {
const { mimetype, transactionId, metadata, name, userId } = req.body;

const extension = mimetype.split('/')[1];
const path = `${name}.${extension}`;

await sendToExchangeTemplate(
JSON.stringify({
path,
userId,
transactionId,
metadata,
}),
'image'
);

res.json({
code: 'vitruveo.studio.api.templates.image.upload.success',
message: 'Template image upload success',
transaction: transactionApiId,
} as APIResponse<string>);
} catch (error) {
logger('Template image upload failed: %O', error);
res.status(500).json({
code: 'vitruveo.studio.api.templates.image.upload.failed',
message: `Template image upload failed: ${error}`,
args: error,
transaction: transactionApiId,
} as APIResponse);
}
});

route.post('/json/upload', async (req, res) => {
try {
await sendToExchangeTemplate(JSON.stringify(req.body), 'json');

res.json({
code: 'vitruveo.studio.api.templates.json.upload.success',
message: 'Template json upload success',
transaction: nanoid(),
} as APIResponse<string>);
} catch (error) {
logger('Template json upload failed: %O', error);
res.status(500).json({
code: 'vitruveo.studio.api.templates.json.upload.failed',
message: `Template json upload failed: ${error}`,
args: error,
transaction: nanoid(),
} as APIResponse);
}
});

export { route };
8 changes: 8 additions & 0 deletions src/features/templates/controller/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from 'express';
import { route as coreRouter } from './core';

const router = Router();

router.use('/templates', coreRouter);

export { router };
3 changes: 3 additions & 0 deletions src/features/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as controller from './controller';

export { controller };
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ app.use(features.requestConsign.controller.router);
app.use(features.events.controller.router);
app.use(features.dashboard.controller.router);
app.use(features.stores.controller.router);
app.use(features.templates.controller.router);

// start schedules
features.schedules.start();
60 changes: 60 additions & 0 deletions src/services/upload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import debug from 'debug';

import { getChannel, Channel } from '../rabbitmq';
import { captureException } from '../sentry';
import { RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES } from '../../constants';

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

const status: {
channel: Channel | null;
} = {
channel: null,
};

export const sendToExchangeTemplate = async (
message: string,
routingKey: string
) => {
try {
if (!status.channel) {
status.channel = await getChannel();
logger(
'Asserting exchange: %s',
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES
);
status.channel.assertExchange(
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES,
'topic',
{
durable: true,
}
);
}
logger('Sending to template exchange', {
message,
routingKey,
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES,
});
status.channel.publish(
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES,
routingKey,
Buffer.from(message)
);
} catch (error) {
logger('Error sending to exchange: %O', {
error,
message,
routingKey,
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES,
});
captureException(error, {
extra: {
message,
routingKey,
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES,
},
tags: { scope: 'sendToExchangeTemplate' },
});
}
};

0 comments on commit 4eafa24

Please sign in to comment.