Compare commits
2 Commits
develop
...
chore/getR
Author | SHA1 | Date |
---|---|---|
AlexAlexandre | 51b59a3b84 | |
AlexAlexandre | 121b54ea9a |
|
@ -5,8 +5,9 @@ import log from '../../utils/log';
|
|||
import reduxStore from '../createStore';
|
||||
import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles';
|
||||
import protectedFunction from './helpers/protectedFunction';
|
||||
import { IRocketChat } from '../../definitions/IRocketChat';
|
||||
|
||||
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();
|
||||
|
@ -14,7 +15,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;
|
||||
|
@ -22,7 +33,7 @@ export async function onRolesChanged(ddpMessage) {
|
|||
try {
|
||||
const rolesRecord = await rolesCollection.find(_id);
|
||||
try {
|
||||
await db.action(async () => {
|
||||
await db.write(async () => {
|
||||
await rolesRecord.update(u => {
|
||||
u.description = description;
|
||||
});
|
||||
|
@ -33,7 +44,7 @@ export async function onRolesChanged(ddpMessage) {
|
|||
reduxStore.dispatch(updateRoles(_id, description));
|
||||
} catch (err) {
|
||||
try {
|
||||
await db.action(async () => {
|
||||
await db.write(async () => {
|
||||
await rolesCollection.create(post => {
|
||||
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
|
||||
});
|
||||
|
@ -49,7 +60,7 @@ export async function onRolesChanged(ddpMessage) {
|
|||
const rolesCollection = db.get('roles');
|
||||
try {
|
||||
const rolesRecord = await rolesCollection.find(_id);
|
||||
await db.action(async () => {
|
||||
await db.write(async () => {
|
||||
await rolesRecord.destroyPermanently();
|
||||
});
|
||||
reduxStore.dispatch(removeRoles(_id));
|
||||
|
@ -59,7 +70,8 @@ export async function onRolesChanged(ddpMessage) {
|
|||
}
|
||||
}
|
||||
|
||||
export function getRoles() {
|
||||
// TODO - Remove those anys, after decide how type rest api response
|
||||
export function getRoles(this: IRocketChat): Promise<void> {
|
||||
const db = database.active;
|
||||
return new Promise(async resolve => {
|
||||
try {
|
||||
|
@ -73,18 +85,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));
|
||||
let rolesToCreate = roles.filter((i1: any) => !allRolesRecords.find(i2 => i1._id === i2.id));
|
||||
let rolesToUpdate = allRolesRecords.filter(i1 => roles.find((i2: any) => i1.id === i2._id));
|
||||
|
||||
// Create
|
||||
rolesToCreate = rolesToCreate.map(role =>
|
||||
rolesToCreate = rolesToCreate.map((role: any) =>
|
||||
rolesCollections.prepareCreate(
|
||||
protectedFunction(r => {
|
||||
protectedFunction((r: any) => {
|
||||
r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema);
|
||||
Object.assign(r, role);
|
||||
})
|
||||
|
@ -93,9 +105,9 @@ export function getRoles() {
|
|||
|
||||
// Update
|
||||
rolesToUpdate = rolesToUpdate.map(role => {
|
||||
const newRole = roles.find(r => r._id === role.id);
|
||||
const newRole = roles.find((r: any) => r._id === role.id);
|
||||
return role.prepareUpdate(
|
||||
protectedFunction(r => {
|
||||
protectedFunction((r: any) => {
|
||||
Object.assign(r, newRole);
|
||||
})
|
||||
);
|
Loading…
Reference in New Issue