Skip to content

Commit

Permalink
fix: support nanoid triggerAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Oct 19, 2023
1 parent b715cfd commit d45f3fc
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/filters/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Price } from '~/database/entity/price';
import alerts from '~/registries/alerts';

const selectedItemRegex = /\$triggerAlert\((?<uuid>[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}),? ?(?<options>.*)?\)/mi;
// expecting 21 symbols for nanoid
const selectedItemRegexNanoId = /\$triggerAlert\((?<uuid>.{21}),? ?(?<options>.*)?\)/mi;

export const operation: ResponseFilter = {
'$triggerOperation(#)': async function (filter: string, attributes) {
Expand All @@ -21,31 +23,34 @@ export const operation: ResponseFilter = {
return '';
},
'$triggerAlert(#)': async function (filter: string, attributes) {
const match = selectedItemRegex.exec(filter);
if (match && match.groups) {
let customOptions: EmitData['customOptions'] = {};
if (match.groups.options) {
customOptions = JSON.parse(Buffer.from(match.groups.options, 'base64').toString('utf-8'));
info(`Triggering alert ${match.groups.uuid} by command ${attributes.command} with custom options ${JSON.stringify(customOptions)}`);
} else {
info(`Triggering alert ${match.groups.uuid} by command ${attributes.command}`);
}
// TODO: selectedItemRegex is deprecated
for (const regex of [selectedItemRegex, selectedItemRegexNanoId]) {
const match = regex.exec(filter);
if (match && match.groups) {
let customOptions: EmitData['customOptions'] = {};
if (match.groups.options) {
customOptions = JSON.parse(Buffer.from(match.groups.options, 'base64').toString('utf-8'));
info(`Triggering alert ${match.groups.uuid} by command ${attributes.command} with custom options ${JSON.stringify(customOptions)}`);
} else {
info(`Triggering alert ${match.groups.uuid} by command ${attributes.command}`);
}

const price = await AppDataSource.getRepository(Price).findOneBy({ command: attributes.command, enabled: true });
const price = await AppDataSource.getRepository(Price).findOneBy({ command: attributes.command, enabled: true });

await alerts.trigger({
amount: price ? price.price : 0,
currency: 'CZK',
event: 'custom',
alertId: match.groups.uuid,
message: attributes.param || '',
monthsName: '',
name: attributes.command,
tier: null,
recipient: attributes.sender.userName,
customOptions,
});
await alerts.trigger({
amount: price ? price.price : 0,
currency: 'CZK',
event: 'custom',
alertId: match.groups.uuid,
message: attributes.param || '',
monthsName: '',
name: attributes.command,
tier: null,
recipient: attributes.sender.userName,
customOptions,
});
}
return '';
}
return '';
},
};

0 comments on commit d45f3fc

Please sign in to comment.