2017-06-12 21:49:46 +00:00
|
|
|
import deepEqual from 'deep-equal';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database 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';
|
2021-07-22 23:04:56 +00:00
|
|
|
import * as models from './index';
|
2021-06-16 19:19:00 +00:00
|
|
|
import { isRequest, Request } from './request';
|
2019-06-07 15:25:02 +00:00
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
export const name = 'Request Version';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
export const type = 'RequestVersion';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
export const prefix = 'rvr';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-06-12 21:49:46 +00:00
|
|
|
export const canDuplicate = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2017-06-12 21:49:46 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface BaseRequestVersion {
|
|
|
|
compressedRequest: string | null;
|
|
|
|
}
|
2019-06-07 15:25:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isRequestVersion = (model: Pick<BaseModel, 'type'>): model is RequestVersion => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function migrate(doc: RequestVersion) {
|
2017-06-12 21:49:46 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getById(id: string) {
|
|
|
|
return db.get<RequestVersion>(type, id);
|
2017-06-12 21:49:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function create(request: Request) {
|
2021-06-16 19:19:00 +00:00
|
|
|
if (!isRequest(request)) {
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<RequestVersion>(type, {
|
|
|
|
parentId,
|
|
|
|
compressedRequest,
|
|
|
|
});
|
2017-06-12 21:49:46 +00:00
|
|
|
} else {
|
|
|
|
// Re-use the latest version if not modified since
|
|
|
|
return latestRequestVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getLatestByParentId(parentId: string) {
|
|
|
|
return db.getMostRecentlyModified<RequestVersion>(type, { parentId });
|
2017-06-12 21:49:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function restore(requestVersionId: string) {
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
const originalRequest = await models.request.getById(requestPatch._id);
|
2019-06-07 15:25:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
function _diffRequests(rOld: Request | null, rNew: Request) {
|
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;
|
|
|
|
}
|
2019-07-02 19:42:00 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function all() {
|
|
|
|
return db.all<RequestVersion>(type);
|
2019-07-02 19:42:00 +00:00
|
|
|
}
|