From 8237b3e673b482459d0a3d5d77dffbeb67030cc0 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 18 Feb 2022 22:19:33 -0400 Subject: [PATCH] Chore: Migrate getSlashCommands to TS (#3711) * migrate getSlashCommands to TS * use sdk and update getSlashCommands * minor tweak * Remove implicit anys Co-authored-by: Diego Mello --- app/definitions/ISlashCommand.ts | 4 ++ app/lib/methods/getSlashCommands.js | 71 -------------------------- app/lib/methods/getSlashCommands.ts | 78 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 71 deletions(-) delete mode 100644 app/lib/methods/getSlashCommands.js create mode 100644 app/lib/methods/getSlashCommands.ts diff --git a/app/definitions/ISlashCommand.ts b/app/definitions/ISlashCommand.ts index a859448d..96639273 100644 --- a/app/definitions/ISlashCommand.ts +++ b/app/definitions/ISlashCommand.ts @@ -9,4 +9,8 @@ export interface ISlashCommand { appId?: string; } +export interface ISlashCommandResult extends ISlashCommand { + command: string; +} + export type TSlashCommandModel = ISlashCommand & Model; diff --git a/app/lib/methods/getSlashCommands.js b/app/lib/methods/getSlashCommands.js deleted file mode 100644 index 3b65c40e..00000000 --- a/app/lib/methods/getSlashCommands.js +++ /dev/null @@ -1,71 +0,0 @@ -import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; - -import database from '../database'; -import log from '../../utils/log'; -import protectedFunction from './helpers/protectedFunction'; - -export default function () { - const db = database.active; - return new Promise(async resolve => { - try { - // RC 0.60.2 - const result = await this.sdk.get('commands.list'); - - if (!result.success) { - console.log(result); - return resolve(); - } - - const { commands } = result; - - if (commands && commands.length) { - await db.action(async () => { - const slashCommandsCollection = db.get('slash_commands'); - const allSlashCommandsRecords = await slashCommandsCollection.query().fetch(); - - // filter slash commands - let slashCommandsToCreate = commands.filter(i1 => !allSlashCommandsRecords.find(i2 => i1.command === i2.id)); - let slashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => commands.find(i2 => i1.id === i2.command)); - let slashCommandsToDelete = allSlashCommandsRecords.filter( - i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id) - ); - - // Create - slashCommandsToCreate = slashCommandsToCreate.map(command => - slashCommandsCollection.prepareCreate( - protectedFunction(s => { - s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema); - Object.assign(s, command); - }) - ) - ); - - // Update - slashCommandsToUpdate = slashCommandsToUpdate.map(command => { - const newCommand = commands.find(s => s.command === command.id); - return command.prepareUpdate( - protectedFunction(s => { - Object.assign(s, newCommand); - }) - ); - }); - - // Delete - slashCommandsToDelete = slashCommandsToDelete.map(command => command.prepareDestroyPermanently()); - - const allRecords = [...slashCommandsToCreate, ...slashCommandsToUpdate, ...slashCommandsToDelete]; - - try { - await db.batch(...allRecords); - } catch (e) { - log(e); - } - return allRecords.length; - }); - } - } catch (e) { - log(e); - return resolve(); - } - }); -} diff --git a/app/lib/methods/getSlashCommands.ts b/app/lib/methods/getSlashCommands.ts new file mode 100644 index 00000000..ed9f3778 --- /dev/null +++ b/app/lib/methods/getSlashCommands.ts @@ -0,0 +1,78 @@ +import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; + +import database from '../database'; +import log from '../../utils/log'; +import protectedFunction from './helpers/protectedFunction'; +import { ISlashCommandResult, TSlashCommandModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; + +export default function getSlashCommands() { + const db = database.active; + return new Promise(async resolve => { + try { + // RC 0.60.2 + // @ts-ignore + const result = await sdk.get('commands.list'); + + if (!result.success) { + return resolve(); + } + // @ts-ignore + const { commands } = result; + if (commands && commands.length) { + await db.write(async () => { + const slashCommandsCollection = db.get('slash_commands'); + const allSlashCommandsRecords = await slashCommandsCollection.query().fetch(); + + // filter slash commands + const filteredSlashCommandsToCreate = commands.filter( + (i1: ISlashCommandResult) => !allSlashCommandsRecords.find(i2 => i1.command === i2.id) + ); + const filteredSlashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => + commands.find((i2: ISlashCommandResult) => i1.id === i2.command) + ); + const filteredSlashCommandsToDelete = allSlashCommandsRecords.filter( + i1 => + !filteredSlashCommandsToCreate.find((i2: ISlashCommandResult) => i2.command === i1.id) && + !filteredSlashCommandsToUpdate.find(i2 => i2.id === i1.id) + ); + + // Create + const slashCommandsToCreate = filteredSlashCommandsToCreate.map((command: ISlashCommandResult) => + slashCommandsCollection.prepareCreate( + protectedFunction((s: TSlashCommandModel) => { + s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema); + Object.assign(s, command); + }) + ) + ); + + // Update + const slashCommandsToUpdate = filteredSlashCommandsToUpdate.map(command => { + const newCommand = commands.find((s: ISlashCommandResult) => s.command === command.id); + return command.prepareUpdate( + protectedFunction((s: TSlashCommandModel) => { + Object.assign(s, newCommand); + }) + ); + }); + + // Delete + const slashCommandsToDelete = filteredSlashCommandsToDelete.map(command => command.prepareDestroyPermanently()); + + const allRecords = [...slashCommandsToCreate, ...slashCommandsToUpdate, ...slashCommandsToDelete]; + + try { + await db.batch(...allRecords); + } catch (e) { + log(e); + } + return allRecords.length; + }); + } + } catch (e) { + log(e); + return resolve(); + } + }); +}