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 reduxStore from '../createStore';
|
||||||
import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles';
|
import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles';
|
||||||
import protectedFunction from './helpers/protectedFunction';
|
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 db = database.active;
|
||||||
const rolesCollection = db.get('roles');
|
const rolesCollection = db.get('roles');
|
||||||
const allRoles = await rolesCollection.query().fetch();
|
const allRoles = await rolesCollection.query().fetch();
|
||||||
|
@ -14,7 +15,17 @@ export async function setRoles() {
|
||||||
reduxStore.dispatch(setRolesAction(parsed));
|
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];
|
const { type, _id, description } = ddpMessage.fields.args[0];
|
||||||
if (/changed/.test(type)) {
|
if (/changed/.test(type)) {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
|
@ -22,7 +33,7 @@ export async function onRolesChanged(ddpMessage) {
|
||||||
try {
|
try {
|
||||||
const rolesRecord = await rolesCollection.find(_id);
|
const rolesRecord = await rolesCollection.find(_id);
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await rolesRecord.update(u => {
|
await rolesRecord.update(u => {
|
||||||
u.description = description;
|
u.description = description;
|
||||||
});
|
});
|
||||||
|
@ -33,7 +44,7 @@ export async function onRolesChanged(ddpMessage) {
|
||||||
reduxStore.dispatch(updateRoles(_id, description));
|
reduxStore.dispatch(updateRoles(_id, description));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await rolesCollection.create(post => {
|
await rolesCollection.create(post => {
|
||||||
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
|
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
|
||||||
});
|
});
|
||||||
|
@ -49,7 +60,7 @@ export async function onRolesChanged(ddpMessage) {
|
||||||
const rolesCollection = db.get('roles');
|
const rolesCollection = db.get('roles');
|
||||||
try {
|
try {
|
||||||
const rolesRecord = await rolesCollection.find(_id);
|
const rolesRecord = await rolesCollection.find(_id);
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await rolesRecord.destroyPermanently();
|
await rolesRecord.destroyPermanently();
|
||||||
});
|
});
|
||||||
reduxStore.dispatch(removeRoles(_id));
|
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;
|
const db = database.active;
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
try {
|
try {
|
||||||
|
@ -73,18 +85,18 @@ export function getRoles() {
|
||||||
const { roles } = result;
|
const { roles } = result;
|
||||||
|
|
||||||
if (roles && roles.length) {
|
if (roles && roles.length) {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
const rolesCollections = db.get('roles');
|
const rolesCollections = db.get('roles');
|
||||||
const allRolesRecords = await rolesCollections.query().fetch();
|
const allRolesRecords = await rolesCollections.query().fetch();
|
||||||
|
|
||||||
// filter roles
|
// filter roles
|
||||||
let rolesToCreate = roles.filter(i1 => !allRolesRecords.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 => i1.id === i2._id));
|
let rolesToUpdate = allRolesRecords.filter(i1 => roles.find((i2: any) => i1.id === i2._id));
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
rolesToCreate = rolesToCreate.map(role =>
|
rolesToCreate = rolesToCreate.map((role: any) =>
|
||||||
rolesCollections.prepareCreate(
|
rolesCollections.prepareCreate(
|
||||||
protectedFunction(r => {
|
protectedFunction((r: any) => {
|
||||||
r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema);
|
r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema);
|
||||||
Object.assign(r, role);
|
Object.assign(r, role);
|
||||||
})
|
})
|
||||||
|
@ -93,9 +105,9 @@ export function getRoles() {
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
rolesToUpdate = rolesToUpdate.map(role => {
|
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(
|
return role.prepareUpdate(
|
||||||
protectedFunction(r => {
|
protectedFunction((r: any) => {
|
||||||
Object.assign(r, newRole);
|
Object.assign(r, newRole);
|
||||||
})
|
})
|
||||||
);
|
);
|
Loading…
Reference in New Issue