Skip to content

Commit

Permalink
chore: migrate loadMissedMessages to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlsilva committed Feb 9, 2022
1 parent 5997c7a commit 385d595
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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<void> {
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) {
Expand Down

0 comments on commit 385d595

Please sign in to comment.