From 113cfb043e4af1bb760988face72ff6f7fba4310 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Mon, 21 Feb 2022 11:26:10 -0300 Subject: [PATCH] Chore: Migrate methods/getRoles to Typescript (#3741) * chore: migrate getRoles to ts * chore: removing unused const * chore: minor tweak * Type batch Co-authored-by: Diego Mello --- app/lib/methods/{getRoles.js => getRoles.ts} | 38 +++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) rename app/lib/methods/{getRoles.js => getRoles.ts} (71%) diff --git a/app/lib/methods/getRoles.js b/app/lib/methods/getRoles.ts similarity index 71% rename from app/lib/methods/getRoles.js rename to app/lib/methods/getRoles.ts index 5f9c91ee5..6add9e897 100644 --- a/app/lib/methods/getRoles.js +++ b/app/lib/methods/getRoles.ts @@ -1,4 +1,5 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import Model from '@nozbe/watermelondb/Model'; import database from '../database'; import { getRoleById } from '../database/services/Role'; @@ -6,8 +7,10 @@ import log from '../../utils/log'; import { store as reduxStore } from '../auxStore'; import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles'; import protectedFunction from './helpers/protectedFunction'; +import { TRoleModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; -export async function setRoles() { +export async function setRoles(): Promise { const db = database.active; const rolesCollection = db.get('roles'); const allRoles = await rolesCollection.query().fetch(); @@ -15,7 +18,17 @@ export async function setRoles() { reduxStore.dispatch(setRolesAction(parsed)); } -export async function onRolesChanged(ddpMessage) { +interface IRolesChanged { + fields: { + args: { + type: string; + _id: string; + description: string; + }[]; + }; +} + +export async function onRolesChanged(ddpMessage: IRolesChanged): Promise { const { type, _id, description } = ddpMessage.fields.args[0]; if (/changed/.test(type)) { const db = database.active; @@ -42,7 +55,6 @@ export async function onRolesChanged(ddpMessage) { } if (/removed/.test(type)) { const db = database.active; - const rolesCollection = db.get('roles'); try { const roleRecord = await getRoleById(_id); if (roleRecord) { @@ -57,12 +69,12 @@ export async function onRolesChanged(ddpMessage) { } } -export function getRoles() { +export function getRoles(): Promise { const db = database.active; return new Promise(async resolve => { try { // RC 0.70.0 - const result = await this.sdk.get('roles.list'); + const result = await sdk.get('roles.list'); if (!result.success) { return resolve(); @@ -71,18 +83,18 @@ export function getRoles() { const { roles } = result; if (roles && roles.length) { - await db.action(async () => { + await db.write(async () => { const rolesCollections = db.get('roles'); const allRolesRecords = await rolesCollections.query().fetch(); // filter roles - let rolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id)); - let rolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id)); + const filteredRolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id)); + const filteredRolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id)); // Create - rolesToCreate = rolesToCreate.map(role => + const rolesToCreate = filteredRolesToCreate.map(role => rolesCollections.prepareCreate( - protectedFunction(r => { + protectedFunction((r: TRoleModel) => { r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema); Object.assign(r, role); }) @@ -90,16 +102,16 @@ export function getRoles() { ); // Update - rolesToUpdate = rolesToUpdate.map(role => { + const rolesToUpdate = filteredRolesToUpdate.map(role => { const newRole = roles.find(r => r._id === role.id); return role.prepareUpdate( - protectedFunction(r => { + protectedFunction((r: TRoleModel) => { Object.assign(r, newRole); }) ); }); - const allRecords = [...rolesToCreate, ...rolesToUpdate]; + const allRecords: Model[] = [...rolesToCreate, ...rolesToUpdate]; try { await db.batch(...allRecords);