2020-05-25 21:30:55 -05:00
|
|
|
import Q from "q";
|
|
|
|
import * as ko from "knockout";
|
|
|
|
import * as Constants from "../../Common/Constants";
|
|
|
|
import DocumentId from "./DocumentId";
|
|
|
|
import * as DataModels from "../../Contracts/DataModels";
|
|
|
|
import * as ViewModels from "../../Contracts/ViewModels";
|
|
|
|
import { extractPartitionKey } from "@azure/cosmos";
|
2020-07-21 13:50:51 -05:00
|
|
|
import ConflictsTab from "../Tabs/ConflictsTab";
|
2020-12-16 15:27:17 -08:00
|
|
|
import { readDocument } from "../../Common/dataAccess/readDocument";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
export default class ConflictId {
|
2020-07-21 13:50:51 -05:00
|
|
|
public container: ConflictsTab;
|
2020-05-25 21:30:55 -05:00
|
|
|
public rid: string;
|
|
|
|
public self: string;
|
|
|
|
public ts: string;
|
|
|
|
public id: ko.Observable<string>;
|
|
|
|
public partitionKeyProperty: string;
|
|
|
|
public partitionKey: DataModels.PartitionKey;
|
|
|
|
public partitionKeyValue: any;
|
|
|
|
public stringPartitionKeyValue: string;
|
|
|
|
public resourceId: string;
|
|
|
|
public resourceType: string;
|
|
|
|
public operationType: string;
|
|
|
|
public content: string;
|
|
|
|
public parsedContent: any;
|
|
|
|
public isDirty: ko.Observable<boolean>;
|
|
|
|
|
2020-07-21 13:50:51 -05:00
|
|
|
constructor(container: ConflictsTab, data: any) {
|
2020-05-25 21:30:55 -05:00
|
|
|
this.container = container;
|
|
|
|
this.self = data._self;
|
|
|
|
this.rid = data._rid;
|
|
|
|
this.ts = data._ts;
|
|
|
|
this.resourceId = data.resourceId;
|
|
|
|
this.resourceType = data.resourceType;
|
|
|
|
this.operationType = data.operationType;
|
|
|
|
this.content = data.content;
|
|
|
|
if (this.content) {
|
|
|
|
try {
|
|
|
|
this.parsedContent = JSON.parse(this.content);
|
|
|
|
} catch (error) {
|
|
|
|
//TODO Handle this error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.partitionKeyProperty = container && container.partitionKeyProperty;
|
|
|
|
this.partitionKey = container && container.partitionKey;
|
|
|
|
this.partitionKeyValue = extractPartitionKey(this.parsedContent, this.partitionKey as any);
|
|
|
|
this.stringPartitionKeyValue = this.getPartitionKeyValueAsString();
|
|
|
|
this.id = ko.observable(data.id);
|
|
|
|
this.isDirty = ko.observable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public click() {
|
|
|
|
if (
|
|
|
|
!this.container.isEditorDirty() ||
|
|
|
|
window.confirm("Your unsaved changes will be lost. Do you want to continue?")
|
|
|
|
) {
|
|
|
|
this.loadConflict();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:27:17 -08:00
|
|
|
public async loadConflict(): Promise<void> {
|
2020-05-25 21:30:55 -05:00
|
|
|
this.container.selectedConflictId(this);
|
|
|
|
|
|
|
|
if (this.operationType === Constants.ConflictOperationType.Create) {
|
|
|
|
this.container.initDocumentEditorForCreate(this, this.content);
|
2020-12-16 15:27:17 -08:00
|
|
|
return;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.container.loadingConflictData(true);
|
|
|
|
|
2020-12-16 15:27:17 -08:00
|
|
|
try {
|
|
|
|
const currentDocumentContent = await readDocument(
|
|
|
|
this.container.collection,
|
|
|
|
this.buildDocumentIdFromConflict(this.partitionKeyValue)
|
|
|
|
);
|
2020-07-27 12:58:27 -05:00
|
|
|
|
2020-12-16 15:27:17 -08:00
|
|
|
if (this.operationType === Constants.ConflictOperationType.Replace) {
|
|
|
|
this.container.initDocumentEditorForReplace(this, this.content, currentDocumentContent);
|
|
|
|
} else {
|
|
|
|
this.container.initDocumentEditorForDelete(this, currentDocumentContent);
|
2020-07-27 12:58:27 -05:00
|
|
|
}
|
2020-12-16 15:27:17 -08:00
|
|
|
} catch (error) {
|
|
|
|
// Document could be deleted
|
|
|
|
if (
|
|
|
|
error &&
|
|
|
|
error.code === Constants.HttpStatusCodes.NotFound &&
|
|
|
|
this.operationType === Constants.ConflictOperationType.Delete
|
|
|
|
) {
|
|
|
|
this.container.initDocumentEditorForNoOp(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.container.loadingConflictData(false);
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPartitionKeyValueAsString(): string {
|
|
|
|
const partitionKeyValue = this.partitionKeyValue;
|
|
|
|
const typeOfPartitionKeyValue = typeof partitionKeyValue;
|
|
|
|
|
|
|
|
if (partitionKeyValue === undefined || partitionKeyValue === null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeOfPartitionKeyValue === "string") {
|
|
|
|
return partitionKeyValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(partitionKeyValue)) {
|
|
|
|
return partitionKeyValue.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(partitionKeyValue);
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
public buildDocumentIdFromConflict(partitionKeyValue: any): DocumentId {
|
2020-05-25 21:30:55 -05:00
|
|
|
const conflictDocumentRid = Constants.HashRoutePrefixes.docsWithIds(
|
|
|
|
this.container.collection.getDatabase().rid,
|
|
|
|
this.container.collection.rid,
|
|
|
|
this.resourceId
|
|
|
|
);
|
|
|
|
const partitionKeyValueResolved = partitionKeyValue || this.partitionKeyValue;
|
|
|
|
let id = this.resourceId;
|
|
|
|
if (this.parsedContent) {
|
|
|
|
try {
|
|
|
|
id = this.parsedContent.id;
|
|
|
|
} catch (error) {
|
|
|
|
//TODO Handle this error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const documentId = new DocumentId(
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
_rid: this.resourceId,
|
|
|
|
_self: conflictDocumentRid,
|
|
|
|
id,
|
|
|
|
partitionKeyValue: partitionKeyValueResolved,
|
|
|
|
partitionKeyProperty: this.partitionKeyProperty,
|
2021-01-20 09:15:01 -06:00
|
|
|
partitionKey: this.partitionKey,
|
2020-05-25 21:30:55 -05:00
|
|
|
},
|
|
|
|
partitionKeyValueResolved
|
|
|
|
);
|
|
|
|
|
|
|
|
documentId.partitionKeyProperty = this.partitionKeyProperty;
|
|
|
|
documentId.partitionKey = this.partitionKey;
|
|
|
|
|
|
|
|
return documentId;
|
|
|
|
}
|
|
|
|
}
|