2019-06-07 15:25:02 +00:00
|
|
|
// @flow
|
2017-06-12 21:49:46 +00:00
|
|
|
import deepEqual from 'deep-equal';
|
|
|
|
import * as models from './index';
|
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { compressObject, decompressObject } from '../common/misc';
|
2019-06-07 15:25:02 +00:00
|
|
|
import type { BaseModel } from './index';
|
|
|
|
import type { Request } from './request';
|
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
export const name = 'Request Version';
|
|
|
|
export const type = 'RequestVersion';
|
|
|
|
export const prefix = 'rvr';
|
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2017-06-12 21:49:46 +00:00
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
type BaseRequestVersion = {
|
|
|
|
compressedRequest: string | null,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestVersion = BaseModel & BaseRequestVersion;
|
|
|
|
|
2018-03-06 06:02:31 +00:00
|
|
|
const FIELDS_TO_IGNORE = [
|
2017-06-12 21:49:46 +00:00
|
|
|
'_id',
|
|
|
|
'type',
|
|
|
|
'created',
|
|
|
|
'modified',
|
|
|
|
'metaSortKey',
|
|
|
|
'description',
|
2018-03-06 06:02:31 +00:00
|
|
|
'parentId',
|
2018-12-12 17:36:11 +00:00
|
|
|
'name',
|
2017-06-12 21:49:46 +00:00
|
|
|
];
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init() {
|
2017-06-12 21:49:46 +00:00
|
|
|
return {
|
2018-12-12 17:36:11 +00:00
|
|
|
compressedRequest: null,
|
2017-06-12 21:49:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export function migrate(doc: RequestVersion): RequestVersion {
|
2017-06-12 21:49:46 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export function getById(id: string): Promise<RequestVersion | null> {
|
2017-06-12 21:49:46 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export async function create(request: Request): Promise<RequestVersion> {
|
2017-06-12 21:49:46 +00:00
|
|
|
if (!request.type === models.request.type) {
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error(`New ${type} was not given a valid ${models.request.type} instance`);
|
2017-06-12 21:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const parentId = request._id;
|
2019-06-07 15:25:02 +00:00
|
|
|
const latestRequestVersion: RequestVersion | null = await getLatestByParentId(parentId);
|
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
const latestRequest = latestRequestVersion
|
2017-06-30 03:30:22 +00:00
|
|
|
? decompressObject(latestRequestVersion.compressedRequest)
|
2017-06-12 21:49:46 +00:00
|
|
|
: null;
|
|
|
|
|
|
|
|
const hasChanged = _diffRequests(latestRequest, request);
|
2019-06-07 15:25:02 +00:00
|
|
|
console.log('HAS CHANGED', { latestRequestVersion, request });
|
|
|
|
if (hasChanged || !latestRequestVersion) {
|
2017-06-12 21:49:46 +00:00
|
|
|
// Create a new version if the request has been modified
|
2017-06-30 03:30:22 +00:00
|
|
|
const compressedRequest = compressObject(request);
|
2018-06-25 17:42:50 +00:00
|
|
|
return db.docCreate(type, { parentId, compressedRequest });
|
2017-06-12 21:49:46 +00:00
|
|
|
} else {
|
|
|
|
// Re-use the latest version if not modified since
|
|
|
|
return latestRequestVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export function getLatestByParentId(parentId: string): Promise<RequestVersion | null> {
|
2018-06-25 17:42:50 +00:00
|
|
|
return db.getMostRecentlyModified(type, { parentId });
|
2017-06-12 21:49:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export async function restore(requestVersionId: string): Promise<Request | null> {
|
2017-06-12 21:49:46 +00:00
|
|
|
const requestVersion = await getById(requestVersionId);
|
|
|
|
|
|
|
|
// Older responses won't have versions saved with them
|
|
|
|
if (!requestVersion) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-06 06:02:31 +00:00
|
|
|
const requestPatch = decompressObject(requestVersion.compressedRequest);
|
2019-06-07 15:25:02 +00:00
|
|
|
const originalRequest: Request | null = await models.request.getById(requestPatch._id);
|
|
|
|
|
|
|
|
if (!originalRequest) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-06 06:02:31 +00:00
|
|
|
|
|
|
|
// Only restore fields that aren't blacklisted
|
|
|
|
for (const field of FIELDS_TO_IGNORE) {
|
|
|
|
delete requestPatch[field];
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.request.update(originalRequest, requestPatch);
|
2017-06-12 21:49:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
function _diffRequests(rOld: Request | null, rNew: Request): boolean {
|
2017-06-12 21:49:46 +00:00
|
|
|
if (!rOld) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of Object.keys(rOld)) {
|
|
|
|
// Skip fields that aren't useful
|
2018-03-06 06:02:31 +00:00
|
|
|
if (FIELDS_TO_IGNORE.includes(key)) {
|
2017-06-12 21:49:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!deepEqual(rOld[key], rNew[key])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|