Fixing issues when adding and deleting records from the input record linking list.
This commit is contained in:
parent
611793b32e
commit
a891149b5b
|
@ -83,23 +83,23 @@ class LinkController {
|
||||||
/**
|
/**
|
||||||
* Utility function for main getLinkDocuments function - refer to it for functionality.
|
* Utility function for main getLinkDocuments function - refer to it for functionality.
|
||||||
*/
|
*/
|
||||||
getRecordLinkDocs(recordId, includeDocs = IncludeDocs.EXCLUDE) {
|
getRecordLinkDocs(recordId) {
|
||||||
return getLinkDocuments({
|
return getLinkDocuments({
|
||||||
instanceId: this._instanceId,
|
instanceId: this._instanceId,
|
||||||
modelId: this._modelId,
|
modelId: this._modelId,
|
||||||
recordId,
|
recordId,
|
||||||
includeDocs,
|
includeDocs: IncludeDocs.INCLUDE,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function for main getLinkDocuments function - refer to it for functionality.
|
* Utility function for main getLinkDocuments function - refer to it for functionality.
|
||||||
*/
|
*/
|
||||||
getModelLinkDocs(includeDocs = IncludeDocs.EXCLUDE) {
|
getModelLinkDocs() {
|
||||||
return getLinkDocuments({
|
return getLinkDocuments({
|
||||||
instanceId: this._instanceId,
|
instanceId: this._instanceId,
|
||||||
modelId: this._modelId,
|
modelId: this._modelId,
|
||||||
includeDocs,
|
includeDocs: IncludeDocs.INCLUDE,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,21 +116,23 @@ class LinkController {
|
||||||
const record = this._record
|
const record = this._record
|
||||||
const operations = []
|
const operations = []
|
||||||
// get link docs to compare against
|
// get link docs to compare against
|
||||||
const linkVals = await this.getRecordLinkDocs(record._id)
|
const linkDocs = await this.getRecordLinkDocs(record._id)
|
||||||
for (let fieldName of Object.keys(model.schema)) {
|
for (let fieldName of Object.keys(model.schema)) {
|
||||||
// get the links this record wants to make
|
// get the links this record wants to make
|
||||||
const recordField = record[fieldName]
|
const recordField = record[fieldName]
|
||||||
const field = model.schema[fieldName]
|
const field = model.schema[fieldName]
|
||||||
if (
|
if (field.type === "link" && recordField != null) {
|
||||||
field.type === "link" &&
|
|
||||||
recordField != null &&
|
|
||||||
recordField.length !== 0
|
|
||||||
) {
|
|
||||||
// check which links actual pertain to the update in this record
|
// check which links actual pertain to the update in this record
|
||||||
let linkDocIds = linkVals.filter(
|
const thisFieldLinkDocs = linkDocs.filter(
|
||||||
linkVal => linkVal.fieldName === fieldName
|
linkDoc =>
|
||||||
|
linkDoc.doc1.fieldName === fieldName ||
|
||||||
|
linkDoc.doc2.fieldName === fieldName
|
||||||
)
|
)
|
||||||
linkDocIds = linkDocIds.map(linkVal => linkVal.id)
|
const linkDocIds = thisFieldLinkDocs.map(linkDoc => {
|
||||||
|
return linkDoc.doc1.recordId === record._id
|
||||||
|
? linkDoc.doc2.recordId
|
||||||
|
: linkDoc.doc1.recordId
|
||||||
|
})
|
||||||
// iterate through the link IDs in the record field, see if any don't exist already
|
// iterate through the link IDs in the record field, see if any don't exist already
|
||||||
for (let linkId of recordField) {
|
for (let linkId of recordField) {
|
||||||
if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {
|
if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {
|
||||||
|
@ -145,14 +147,19 @@ class LinkController {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// work out any that need to be deleted
|
|
||||||
const toDeleteIds = linkDocIds.filter(
|
|
||||||
id => recordField.indexOf(id) === -1
|
|
||||||
)
|
|
||||||
operations.concat(
|
|
||||||
toDeleteIds.map(id => ({ _id: id, _deleted: true }))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
// find the docs that need to be deleted
|
||||||
|
let toDeleteDocs = thisFieldLinkDocs
|
||||||
|
.filter(doc => {
|
||||||
|
let correctDoc =
|
||||||
|
doc.doc1.fieldName === fieldName ? doc.doc2 : doc.doc1
|
||||||
|
return recordField.indexOf(correctDoc.recordId) === -1
|
||||||
|
})
|
||||||
|
.map(doc => {
|
||||||
|
return { ...doc, _deleted: true }
|
||||||
|
})
|
||||||
|
// now add the docs to be deleted to the bulk operation
|
||||||
|
operations.push(...toDeleteDocs)
|
||||||
// replace this field with a simple entry to denote there are links
|
// replace this field with a simple entry to denote there are links
|
||||||
delete record[fieldName]
|
delete record[fieldName]
|
||||||
}
|
}
|
||||||
|
@ -170,10 +177,7 @@ class LinkController {
|
||||||
async recordDeleted() {
|
async recordDeleted() {
|
||||||
const record = this._record
|
const record = this._record
|
||||||
// need to get the full link docs to be be able to delete it
|
// need to get the full link docs to be be able to delete it
|
||||||
const linkDocs = await this.getRecordLinkDocs(
|
const linkDocs = await this.getRecordLinkDocs(record._id)
|
||||||
record._id,
|
|
||||||
IncludeDocs.INCLUDE
|
|
||||||
)
|
|
||||||
if (linkDocs.length === 0) {
|
if (linkDocs.length === 0) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -195,7 +199,7 @@ class LinkController {
|
||||||
async removeFieldFromModel(fieldName) {
|
async removeFieldFromModel(fieldName) {
|
||||||
let oldModel = this._oldModel
|
let oldModel = this._oldModel
|
||||||
let field = oldModel.schema[fieldName]
|
let field = oldModel.schema[fieldName]
|
||||||
const linkDocs = await this.getModelLinkDocs(IncludeDocs.INCLUDE)
|
const linkDocs = await this.getModelLinkDocs()
|
||||||
let toDelete = linkDocs.filter(linkDoc => {
|
let toDelete = linkDocs.filter(linkDoc => {
|
||||||
let correctFieldName =
|
let correctFieldName =
|
||||||
linkDoc.doc1.modelId === oldModel._id
|
linkDoc.doc1.modelId === oldModel._id
|
||||||
|
@ -283,7 +287,7 @@ class LinkController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// need to get the full link docs to delete them
|
// need to get the full link docs to delete them
|
||||||
const linkDocs = await this.getModelLinkDocs(IncludeDocs.INCLUDE)
|
const linkDocs = await this.getModelLinkDocs()
|
||||||
if (linkDocs.length === 0) {
|
if (linkDocs.length === 0) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue