2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2021-02-10 13:35:44 +00:00
|
|
|
import lt from 'semver/functions/lt';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2021-02-10 13:35:44 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2019-04-26 20:54:58 +00:00
|
|
|
import reduxStore from '../createStore';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const getUpdatedSince = (allRecords) => {
|
|
|
|
try {
|
|
|
|
if (!allRecords.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const ordered = orderBy(allRecords.filter(item => item._updatedAt !== null), ['_updatedAt'], ['desc']);
|
|
|
|
return ordered && ordered[0]._updatedAt.toISOString();
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return null;
|
2019-04-26 20:54:58 +00:00
|
|
|
};
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const updatePermissions = async({ update = [], remove = [], allRecords }) => {
|
|
|
|
if (!((update && update.length) || (remove && remove.length))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = database.active;
|
|
|
|
const permissionsCollection = db.collections.get('permissions');
|
|
|
|
|
|
|
|
// filter permissions
|
|
|
|
let permissionsToCreate = [];
|
|
|
|
let permissionsToUpdate = [];
|
|
|
|
let permissionsToDelete = [];
|
|
|
|
|
|
|
|
// Create or update
|
|
|
|
if (update && update.length) {
|
|
|
|
permissionsToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id));
|
|
|
|
permissionsToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
|
|
|
|
permissionsToCreate = permissionsToCreate.map(permission => permissionsCollection.prepareCreate(protectedFunction((p) => {
|
|
|
|
p._raw = sanitizedRaw({ id: permission._id }, permissionsCollection.schema);
|
|
|
|
Object.assign(p, permission);
|
|
|
|
})));
|
|
|
|
permissionsToUpdate = permissionsToUpdate.map((permission) => {
|
|
|
|
const newPermission = update.find(p => p._id === permission.id);
|
|
|
|
return permission.prepareUpdate(protectedFunction((p) => {
|
|
|
|
Object.assign(p, newPermission);
|
|
|
|
}));
|
2019-04-26 20:54:58 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
// Delete
|
|
|
|
if (remove && remove.length) {
|
|
|
|
permissionsToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
permissionsToDelete = permissionsToDelete.map(permission => permission.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
|
|
|
const batch = [
|
|
|
|
...permissionsToCreate,
|
|
|
|
...permissionsToUpdate,
|
|
|
|
...permissionsToDelete
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...batch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-04-26 20:54:58 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
export default function() {
|
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
|
|
|
const permissionsCollection = db.collections.get('permissions');
|
|
|
|
const allRecords = await permissionsCollection.query().fetch();
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
// if server version is lower than 0.73.0, fetches from old api
|
2021-02-10 13:35:44 +00:00
|
|
|
if (serverVersion && lt(serverVersion, '0.73.0')) {
|
2019-06-17 13:57:07 +00:00
|
|
|
// RC 0.66.0
|
|
|
|
const result = await this.sdk.get('permissions.list');
|
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
await updatePermissions({ update: result.permissions, allRecords });
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-04-26 20:54:58 +00:00
|
|
|
});
|
2019-06-17 13:57:07 +00:00
|
|
|
} else {
|
|
|
|
const params = {};
|
2019-09-16 20:26:32 +00:00
|
|
|
const updatedSince = await getUpdatedSince(allRecords);
|
2019-06-17 13:57:07 +00:00
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
|
|
|
// RC 0.73.0
|
|
|
|
const result = await this.sdk.get('permissions.listAll', params);
|
2018-06-18 13:30:36 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
await updatePermissions({ update: result.update, remove: result.delete, allRecords });
|
|
|
|
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 20:54:58 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|