2021-03-08 11:07:40 +13:00
|
|
|
import { addFieldToDocument } from "./documentManager"
|
2021-01-31 02:43:13 +13:00
|
|
|
import PouchDB from "pouchdb"
|
|
|
|
import { I_Blueprint } from "src/interfaces/I_Blueprint"
|
|
|
|
import { I_FieldRelationship } from "src/interfaces/I_FieldRelationship"
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
import { I_ExtraDocumentFields, I_OpenedDocument } from "../../interfaces/I_OpenedDocument"
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
export const single_changeRelationshipToAnotherObject = async (
|
|
|
|
field: I_ExtraDocumentFields,
|
|
|
|
currentDocument:I_OpenedDocument,
|
|
|
|
previouDocument: I_OpenedDocument) => {
|
2021-02-09 15:21:48 +13:00
|
|
|
const currentValue = field.value.value
|
|
|
|
const previousValue = (previouDocument?.extraFields?.find(e => e.id === field.id))?.value.value || ""
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
const BlueprintsDB = new PouchDB("blueprints")
|
|
|
|
const currentBlueprint = await BlueprintsDB.get(currentDocument.type) as I_Blueprint
|
|
|
|
const fieldType = (currentBlueprint.extraFields.find(e => e.id === field.id))?.type
|
|
|
|
|
|
|
|
const updatedDocuments:I_OpenedDocument[] = []
|
|
|
|
|
|
|
|
if (!previousValue && typeof currentValue !== "string" && currentValue) {
|
|
|
|
if (fieldType === "singleToSingleRelationship") {
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await single_addRelationshipToAnotherObject(field, currentValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
if (fieldType === "singleToManyRelationship") {
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await many_addRelationshipToAnotherObject(field, currentValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousValue && typeof currentValue !== "string" && currentValue) {
|
|
|
|
if (fieldType === "singleToSingleRelationship") {
|
2021-05-14 05:51:35 +12:00
|
|
|
const updatedDocument = await single_removeRelationshipFromAnotherObject(currentValue, previousValue)
|
|
|
|
if (updatedDocument) {
|
|
|
|
updatedDocuments.push(updatedDocument)
|
|
|
|
}
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await single_addRelationshipToAnotherObject(field, currentValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
if (fieldType === "singleToManyRelationship") {
|
2021-05-14 05:51:35 +12:00
|
|
|
const removedValues = await many_removeRelationshipFromAnotherObject(previousValue, currentDocument)
|
|
|
|
if (removedValues) {
|
|
|
|
updatedDocuments.push(removedValues)
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await many_addRelationshipToAnotherObject(field, currentValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((previousValue && typeof currentValue === "string") || (previousValue && !currentValue)) {
|
|
|
|
if (fieldType === "singleToSingleRelationship") {
|
2021-05-14 05:51:35 +12:00
|
|
|
const updatedDocument = await single_removeRelationshipFromAnotherObject(currentValue, previousValue)
|
|
|
|
if (updatedDocument) {
|
|
|
|
updatedDocuments.push(updatedDocument)
|
|
|
|
}
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
if (fieldType === "singleToManyRelationship") {
|
2021-05-14 05:51:35 +12:00
|
|
|
const removedValues = await many_removeRelationshipFromAnotherObject(previousValue, currentDocument)
|
|
|
|
if (removedValues) {
|
|
|
|
updatedDocuments.push(removedValues)
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 01:28:33 +12:00
|
|
|
|
|
|
|
await BlueprintsDB.close()
|
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
return updatedDocuments
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
export const single_addRelationshipToAnotherObject = async (
|
2021-01-31 02:43:13 +13:00
|
|
|
field: I_ExtraDocumentFields,
|
|
|
|
currentValue: I_FieldRelationship,
|
|
|
|
currentDocument: I_OpenedDocument
|
|
|
|
) => {
|
|
|
|
const typeToFind = currentValue.type
|
|
|
|
const idToFind = currentValue._id
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
window.FA_dbs[typeToFind] = new PouchDB(typeToFind)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
let pairedDocument = await window.FA_dbs[typeToFind].get(idToFind) as I_OpenedDocument
|
2021-01-31 02:43:13 +13:00
|
|
|
const pairedField = currentValue.pairedField
|
2021-03-08 11:07:40 +13:00
|
|
|
let pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
|
|
|
|
let targetPairedField = pairedDocument.extraFields[pairedFieldIndex]
|
|
|
|
|
|
|
|
// Fix non-existant fields
|
|
|
|
if (!targetPairedField) {
|
|
|
|
await addFieldToDocument(pairedDocument._id, pairedField, typeToFind)
|
2021-04-26 09:01:41 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
pairedDocument = await window.FA_dbs[typeToFind].get(idToFind) as I_OpenedDocument
|
2021-03-08 11:07:40 +13:00
|
|
|
pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
targetPairedField = pairedDocument.extraFields[pairedFieldIndex]
|
|
|
|
}
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-02-09 15:21:48 +13:00
|
|
|
if (!pairedDocument.extraFields[pairedFieldIndex].value) {
|
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value = { value: "", addedValues: "" }
|
|
|
|
}
|
|
|
|
|
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value.value = {
|
2021-01-31 02:43:13 +13:00
|
|
|
_id: currentDocument._id,
|
|
|
|
value: currentDocument._id,
|
|
|
|
type: currentDocument.type,
|
|
|
|
url: `/project/display-content/${currentDocument.type}/${currentDocument._id}`,
|
2021-04-26 09:01:41 +12:00
|
|
|
pairedField: field.id
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await window.FA_dbs[typeToFind].put(pairedDocument)
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
return pairedDocument
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
export const single_removeRelationshipFromAnotherObject = async (
|
2021-05-04 07:49:11 +12:00
|
|
|
currentValue: I_FieldRelationship,
|
2021-01-31 02:43:13 +13:00
|
|
|
previousValue: I_FieldRelationship
|
|
|
|
) => {
|
|
|
|
const typeToFind = previousValue.type
|
|
|
|
const idToFind = previousValue._id
|
|
|
|
|
2021-05-14 05:51:35 +12:00
|
|
|
let pairedDocument = false as unknown as I_OpenedDocument
|
|
|
|
try {
|
|
|
|
window.FA_dbs[typeToFind] = new PouchDB(typeToFind)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
pairedDocument = await window.FA_dbs[typeToFind].get(idToFind)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return pairedDocument
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
const pairedField = previousValue.pairedField
|
|
|
|
const pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
|
2021-05-04 07:49:11 +12:00
|
|
|
if (currentValue?._id !== previousValue?._id) {
|
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value = { value: "", addedValues: "" }
|
|
|
|
}
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await window.FA_dbs[typeToFind].put(pairedDocument)
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
return pairedDocument
|
|
|
|
}
|
|
|
|
|
|
|
|
export const many_changeRelationshipToAnotherObject = async (
|
|
|
|
field: I_ExtraDocumentFields,
|
|
|
|
currentDocument:I_OpenedDocument,
|
2021-04-26 09:01:41 +12:00
|
|
|
previouDocument: I_OpenedDocument
|
|
|
|
) => {
|
2021-02-09 15:21:48 +13:00
|
|
|
const currentValue: I_FieldRelationship[] = (field.value?.value && typeof field.value?.value !== "string") ? field.value.value : []
|
|
|
|
const previousValue: I_FieldRelationship[] = (previouDocument?.extraFields?.find(e => e.id === field.id))?.value?.value || []
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
const BlueprintsDB = new PouchDB("blueprints")
|
|
|
|
const currentBlueprint = await BlueprintsDB.get(currentDocument.type) as I_Blueprint
|
|
|
|
|
|
|
|
const fieldType = (currentBlueprint.extraFields.find(e => e.id === field.id))?.type
|
|
|
|
|
|
|
|
const addedValues = currentValue.filter(val => !previousValue.find(subVal => subVal._id === val._id))
|
|
|
|
const removedValues = previousValue.filter(val => !currentValue.find(subVal => subVal._id === val._id))
|
|
|
|
const updatedDocuments:I_OpenedDocument[] = []
|
|
|
|
|
|
|
|
for (const addedValue of addedValues) {
|
|
|
|
if (fieldType === "manyToManyRelationship") {
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await many_addRelationshipToAnotherObject(field, addedValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldType === "manyToSingleRelationship") {
|
2021-04-26 09:01:41 +12:00
|
|
|
updatedDocuments.push(await single_addRelationshipToAnotherObject(field, addedValue, currentDocument))
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const removedValue of removedValues) {
|
|
|
|
if (fieldType === "manyToManyRelationship") {
|
2021-05-14 05:51:35 +12:00
|
|
|
const removedValues = await many_removeRelationshipFromAnotherObject(removedValue, currentDocument)
|
|
|
|
if (removedValues) {
|
|
|
|
updatedDocuments.push(removedValues)
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldType === "manyToSingleRelationship") {
|
2021-05-04 07:49:11 +12:00
|
|
|
// @ts-ignore
|
2021-05-14 13:43:20 +12:00
|
|
|
const removedValues = await single_removeRelationshipFromAnotherObject({}, removedValue)
|
2021-05-14 05:51:35 +12:00
|
|
|
if (removedValues) {
|
|
|
|
updatedDocuments.push(removedValues)
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:28:33 +12:00
|
|
|
await BlueprintsDB.close()
|
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
return updatedDocuments
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
export const many_addRelationshipToAnotherObject = async (
|
2021-01-31 02:43:13 +13:00
|
|
|
field: I_ExtraDocumentFields,
|
|
|
|
currentValue: I_FieldRelationship,
|
|
|
|
currentDocument: I_OpenedDocument
|
|
|
|
) => {
|
|
|
|
const typeToFind = currentValue.type
|
|
|
|
const idToFind = currentValue._id
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
window.FA_dbs[typeToFind] = new PouchDB(typeToFind)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
let pairedDocument = await window.FA_dbs[typeToFind].get(idToFind) as I_OpenedDocument
|
2021-01-31 02:43:13 +13:00
|
|
|
const pairedField = currentValue.pairedField
|
2021-03-08 11:07:40 +13:00
|
|
|
let pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
|
|
|
|
let targetPairedField = pairedDocument.extraFields[pairedFieldIndex]
|
|
|
|
|
|
|
|
// Fix non-existant fields
|
|
|
|
if (!targetPairedField) {
|
|
|
|
await addFieldToDocument(pairedDocument._id, pairedField, typeToFind)
|
2021-04-26 09:01:41 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
pairedDocument = await window.FA_dbs[typeToFind].get(idToFind) as I_OpenedDocument
|
2021-03-08 11:07:40 +13:00
|
|
|
pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
targetPairedField = pairedDocument.extraFields[pairedFieldIndex]
|
|
|
|
}
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-03-08 11:07:40 +13:00
|
|
|
let pairedFieldValue: I_FieldRelationship[] = targetPairedField.value.value
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
const newValue = {
|
|
|
|
_id: currentDocument._id,
|
|
|
|
value: currentDocument._id,
|
|
|
|
type: currentDocument.type,
|
|
|
|
url: `/project/display-content/${currentDocument.type}/${currentDocument._id}`,
|
2021-04-26 09:01:41 +12:00
|
|
|
pairedField: field.id
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:21:48 +13:00
|
|
|
pairedFieldValue = (Array.isArray(pairedFieldValue)) ? pairedFieldValue : []
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-02-09 15:21:48 +13:00
|
|
|
const valueExistsAlready = (pairedFieldValue.find(e => e._id === newValue._id))
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
if (!valueExistsAlready) {
|
2021-04-26 09:01:41 +12:00
|
|
|
// @ts-ignore
|
2021-02-09 15:21:48 +13:00
|
|
|
pairedFieldValue.push(newValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pairedDocument.extraFields[pairedFieldIndex].value) {
|
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value = { value: [], addedValues: [] }
|
2021-01-31 02:43:13 +13:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:21:48 +13:00
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value.value = pairedFieldValue
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await window.FA_dbs[typeToFind].put(pairedDocument)
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-01-31 02:43:13 +13:00
|
|
|
return pairedDocument
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
export const many_removeRelationshipFromAnotherObject = async (
|
2021-01-31 02:43:13 +13:00
|
|
|
previousValue: I_FieldRelationship,
|
|
|
|
currentDocument: I_OpenedDocument
|
|
|
|
) => {
|
|
|
|
const typeToFind = previousValue.type
|
|
|
|
const idToFind = previousValue._id
|
|
|
|
|
|
|
|
let pairedDocument = false as unknown as I_OpenedDocument
|
2021-02-23 11:30:18 +13:00
|
|
|
try {
|
2021-05-14 05:51:35 +12:00
|
|
|
window.FA_dbs[typeToFind] = new PouchDB(typeToFind)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
pairedDocument = await window.FA_dbs[typeToFind].get(idToFind)
|
2021-02-23 11:30:18 +13:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return pairedDocument
|
|
|
|
}
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
const pairedField = previousValue.pairedField
|
|
|
|
const pairedFieldIndex = pairedDocument.extraFields.findIndex(e => e.id === pairedField)
|
|
|
|
|
2021-02-09 15:21:48 +13:00
|
|
|
const currentValues: I_FieldRelationship[] = pairedDocument.extraFields[pairedFieldIndex].value.value
|
2021-01-31 02:43:13 +13:00
|
|
|
|
|
|
|
const indexToRemove = currentValues.findIndex(e => e._id === currentDocument._id)
|
|
|
|
|
|
|
|
currentValues.splice(indexToRemove, 1)
|
2021-02-09 15:21:48 +13:00
|
|
|
pairedDocument.extraFields[pairedFieldIndex].value.value = currentValues
|
2021-01-31 02:43:13 +13:00
|
|
|
|
2021-05-14 05:51:35 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await window.FA_dbs[typeToFind].put(pairedDocument)
|
2021-01-31 02:43:13 +13:00
|
|
|
return pairedDocument
|
|
|
|
}
|