2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2022-02-21 14:26:10 +00:00
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
2019-04-26 21:15:25 +00:00
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import database from '../database';
|
|
|
|
import { getRoleById } from '../database/services/Role';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from './helpers/log';
|
2022-04-07 16:53:07 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
|
|
|
import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles';
|
|
|
|
import { TRoleModel } from '../../definitions';
|
2022-04-04 19:15:29 +00:00
|
|
|
import sdk from '../services/sdk';
|
2022-04-07 16:53:07 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2019-04-26 21:15:25 +00:00
|
|
|
|
2022-02-21 14:26:10 +00:00
|
|
|
export async function setRoles(): Promise<void> {
|
2021-07-01 16:51:19 +00:00
|
|
|
const db = database.active;
|
2022-02-01 13:39:09 +00:00
|
|
|
const rolesCollection = db.get('roles');
|
2021-07-01 16:51:19 +00:00
|
|
|
const allRoles = await rolesCollection.query().fetch();
|
2023-08-11 20:35:24 +00:00
|
|
|
const parsed = allRoles.reduce((acc, item) => ({ ...acc, [item.id]: item.description || item.name || item.id }), {});
|
2021-07-01 16:51:19 +00:00
|
|
|
reduxStore.dispatch(setRolesAction(parsed));
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:26:10 +00:00
|
|
|
interface IRolesChanged {
|
|
|
|
fields: {
|
|
|
|
args: {
|
|
|
|
type: string;
|
|
|
|
_id: string;
|
|
|
|
description: string;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onRolesChanged(ddpMessage: IRolesChanged): Promise<void> {
|
2021-07-01 16:51:19 +00:00
|
|
|
const { type, _id, description } = ddpMessage.fields.args[0];
|
|
|
|
if (/changed/.test(type)) {
|
|
|
|
const db = database.active;
|
|
|
|
const rolesCollection = db.get('roles');
|
|
|
|
try {
|
2022-02-10 20:16:10 +00:00
|
|
|
const roleRecord = await getRoleById(_id);
|
|
|
|
if (roleRecord) {
|
|
|
|
await db.write(async () => {
|
|
|
|
await roleRecord.update(u => {
|
2021-07-01 16:51:19 +00:00
|
|
|
u.description = description;
|
|
|
|
});
|
|
|
|
});
|
2022-02-10 20:16:10 +00:00
|
|
|
} else {
|
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await rolesCollection.create(post => {
|
2021-07-01 16:51:19 +00:00
|
|
|
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
reduxStore.dispatch(updateRoles(_id, description || _id));
|
2022-02-10 20:16:10 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2021-07-01 16:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (/removed/.test(type)) {
|
|
|
|
const db = database.active;
|
|
|
|
try {
|
2022-02-10 20:16:10 +00:00
|
|
|
const roleRecord = await getRoleById(_id);
|
|
|
|
if (roleRecord) {
|
|
|
|
await db.write(async () => {
|
|
|
|
await roleRecord.destroyPermanently();
|
|
|
|
});
|
|
|
|
reduxStore.dispatch(removeRoles(_id));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2021-07-01 16:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:26:10 +00:00
|
|
|
export function getRoles(): Promise<void> {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async resolve => {
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
|
|
|
// RC 0.70.0
|
2022-02-21 14:26:10 +00:00
|
|
|
const result = await sdk.get('roles.list');
|
2019-04-26 21:15:25 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-04-26 21:15:25 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
const { roles } = result;
|
2019-04-26 21:15:25 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (roles && roles.length) {
|
2022-02-21 14:26:10 +00:00
|
|
|
await db.write(async () => {
|
2021-02-26 16:25:51 +00:00
|
|
|
const rolesCollections = db.get('roles');
|
2021-02-23 18:36:20 +00:00
|
|
|
const allRolesRecords = await rolesCollections.query().fetch();
|
|
|
|
|
|
|
|
// filter roles
|
2022-02-21 14:26:10 +00:00
|
|
|
const filteredRolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id));
|
|
|
|
const filteredRolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id));
|
2021-02-23 18:36:20 +00:00
|
|
|
|
|
|
|
// Create
|
2022-02-21 14:26:10 +00:00
|
|
|
const rolesToCreate = filteredRolesToCreate.map(role =>
|
2021-09-13 20:41:05 +00:00
|
|
|
rolesCollections.prepareCreate(
|
2022-02-21 14:26:10 +00:00
|
|
|
protectedFunction((r: TRoleModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema);
|
|
|
|
Object.assign(r, role);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-02-23 18:36:20 +00:00
|
|
|
|
|
|
|
// Update
|
2022-02-21 14:26:10 +00:00
|
|
|
const rolesToUpdate = filteredRolesToUpdate.map(role => {
|
2021-02-23 18:36:20 +00:00
|
|
|
const newRole = roles.find(r => r._id === role.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return role.prepareUpdate(
|
2022-02-21 14:26:10 +00:00
|
|
|
protectedFunction((r: TRoleModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(r, newRole);
|
|
|
|
})
|
|
|
|
);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2021-02-23 18:36:20 +00:00
|
|
|
|
2022-02-21 14:26:10 +00:00
|
|
|
const allRecords: Model[] = [...rolesToCreate, ...rolesToUpdate];
|
2021-02-23 18:36:20 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await db.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-07-01 16:51:19 +00:00
|
|
|
setRoles();
|
2021-02-23 18:36:20 +00:00
|
|
|
return allRecords.length;
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2021-02-23 18:36:20 +00:00
|
|
|
return resolve();
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-04-26 21:15:25 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2019-04-26 21:15:25 +00:00
|
|
|
}
|