2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-18 13:30:36 +00:00
|
|
|
import defaultPermissions from '../../constants/permissions';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const getLastUpdate = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const setting = database.objects('permissions').sorted('_updatedAt', true)[0];
|
|
|
|
return setting && setting._updatedAt;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-09-28 18:57:29 +00:00
|
|
|
if (!this.ddp) {
|
|
|
|
// TODO: should implement loop or get from rest?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const lastUpdate = getLastUpdate();
|
|
|
|
const result = await (!lastUpdate ? this.ddp.call('permissions/get') : this.ddp.call('permissions/get', new Date(lastUpdate)));
|
|
|
|
const permissions = (result.update || result).filter(permission => defaultPermissions.includes(permission._id));
|
|
|
|
permissions
|
|
|
|
.map((permission) => {
|
|
|
|
permission._updatedAt = new Date();
|
|
|
|
permission.roles = permission.roles.map(role => ({ value: role }));
|
|
|
|
return permission;
|
|
|
|
});
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(
|
|
|
|
() => permissions.forEach(permission => database.create('permissions', permission, true))
|
|
|
|
)
|
|
|
|
);
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getPermissions', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|