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 <diegolmello@gmail.com>
This commit is contained in:
parent
bdd1ce4abf
commit
113cfb043e
|
@ -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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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);
|
Loading…
Reference in New Issue