Add bernard sqs msg to add redirect for changed paths (#73)

This commit is contained in:
Niklas Fondberg
2021-10-14 15:55:10 +02:00
committed by GitHub
parent 3cf38453cd
commit 2a8212ff12
7 changed files with 1403 additions and 1 deletions
+38
View File
@@ -0,0 +1,38 @@
import {
SQSClient,
SendMessageCommand,
SendMessageCommandOutput,
SendMessageCommandInput,
} from '@aws-sdk/client-sqs';
export async function sendPathChangedCmd(
oldPath: string,
newPath: string,
): Promise<void> {
const msg = {
args: {
name: 'AddRedirect',
old: oldPath,
new: newPath,
},
class: 'Bernard:Message:DefaultMessage',
timestamp: Math.round(Date.now() / 1000), // Epoch seconds
};
await sendMessage(JSON.stringify(msg), process.env.BERNARD_QUEUE_URL);
}
export async function sendMessage(
body: string,
queueUrl: string,
): Promise<SendMessageCommandOutput | undefined> {
console.log('Senfing', body);
const client = new SQSClient({ region: 'eu-west-1' });
const params: SendMessageCommandInput = {
MessageBody: body,
QueueUrl: queueUrl,
};
const sendMessageCommand = new SendMessageCommand(params);
return client.send(sendMessageCommand);
}
+6
View File
@@ -27,6 +27,7 @@ import {
roundOrDefaultTo,
} from './utils';
import { UserInputError } from 'apollo-server';
import { sendPathChangedCmd } from '../bernard/client';
const MINUTE = 60;
@@ -362,6 +363,10 @@ export class ProductAPI extends BaseSQLDataSource {
): Promise<any[]> {
// Check path and insert new unique path if taken
const uniquePath = await this.getUniqueProductPath(info.path, productId);
const oldProd = await this.getProduct(productId);
if (oldProd.path !== uniquePath) {
sendPathChangedCmd(oldProd.path, uniquePath);
}
const promises = [];
promises.push(
@@ -397,6 +402,7 @@ export class ProductAPI extends BaseSQLDataSource {
info.printFileDpi.toString(),
),
);
return Promise.all(promises);
}