From 385d59541fab9aab463b2cf218b0b530e1de672e Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 9 Feb 2022 16:11:45 -0300 Subject: [PATCH] chore: migrate loadMissedMessages to typescript --- ...adMissedMessages.js => loadMissedMessages.ts} | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) rename app/lib/methods/{loadMissedMessages.js => loadMissedMessages.ts} (55%) diff --git a/app/lib/methods/loadMissedMessages.js b/app/lib/methods/loadMissedMessages.ts similarity index 55% rename from app/lib/methods/loadMissedMessages.js rename to app/lib/methods/loadMissedMessages.ts index 7b2e8c978f..110afc74df 100644 --- a/app/lib/methods/loadMissedMessages.js +++ b/app/lib/methods/loadMissedMessages.ts @@ -1,20 +1,22 @@ import database from '../database'; import log from '../../utils/log'; import updateMessages from './updateMessages'; +import { IRocketChat } from '../../definitions/IRocketChat'; +import { ILastMessage } from '../../definitions'; -const getLastUpdate = async rid => { +const getLastUpdate = async (rid: string) => { try { const db = database.active; const subsCollection = db.get('subscriptions'); const sub = await subsCollection.find(rid); - return sub.lastOpen.toISOString(); + return sub.lastOpen?.toISOString(); } catch (e) { // Do nothing } return null; }; -async function load({ rid: roomId, lastOpen }) { +async function load(this: IRocketChat, { rid: roomId, lastOpen }: { rid: string; lastOpen: string }) { let lastUpdate; if (lastOpen) { lastUpdate = new Date(lastOpen).toISOString(); @@ -26,14 +28,14 @@ async function load({ rid: roomId, lastOpen }) { return result; } -export default function loadMissedMessages(args) { +export default function loadMissedMessages(this: IRocketChat, args: { rid: string; lastOpen: string }): Promise { return new Promise(async (resolve, reject) => { try { const data = await load.call(this, { rid: args.rid, lastOpen: args.lastOpen }); - if (data) { - const { updated, deleted } = data; - await updateMessages({ rid: args.rid, update: updated, remove: deleted }); + const { updated, deleted }: { updated: ILastMessage[]; deleted: ILastMessage[] } = data; + // loaderItem is null only to surpass the obligatoriness of the item in the function, as soon as it is migrated it will not be necessary. + await updateMessages({ rid: args.rid, update: updated, remove: deleted, loaderItem: null }); } resolve(); } catch (e) {