Browse Source

fix(nc-gui): bt cell audit logs update

pull/8901/head
Ramesh Mane 5 months ago
parent
commit
5ddee03e8d
  1. 59
      packages/nocodb/src/db/BaseModelSqlv2.ts

59
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -5141,18 +5141,11 @@ class BaseModelSqlv2 {
_trx: any, _trx: any,
req, req,
updateObj?: Record<string, any>, updateObj?: Record<string, any>,
isLinksUpdate?: boolean,
): Promise<void> { ): Promise<void> {
const id = this._extractPksValues(newData); const id = this._extractPksValues(newData);
let desc = `Record with ID ${id} has been updated in Table ${this.model.title}.`; let desc = `Record with ID ${id} has been updated in Table ${this.model.title}.`;
let details = ''; let details = '';
if (isLinksUpdate) { if (updateObj) {
const key = Object.keys(updateObj)[0];
details = DOMPurify.sanitize(`<span class="">${key}</span>
: <span class="text-decoration-line-through red px-2 lighten-4 black--text">${prevData[key]}</span>
<span class="black--text green lighten-4 px-2">${newData[key]}</span>`);
} else if (updateObj) {
updateObj = await this.model.mapColumnToAlias(this.context, updateObj); updateObj = await this.model.mapColumnToAlias(this.context, updateObj);
for (const k of Object.keys(updateObj)) { for (const k of Object.keys(updateObj)) {
@ -5582,6 +5575,49 @@ class BaseModelSqlv2 {
break; break;
case RelationTypes.BELONGS_TO: case RelationTypes.BELONGS_TO:
{ {
const linkedHmRowObj = await this.dbDriver(childTn)
.select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, rowId))
.first();
const oldRowID = Object.values(linkedHmRowObj)?.[0];
if (oldRowID) {
const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([
await this.dbDriver(childTn)
.select(childTable.displayValue.title)
.where(_wherePk(childTable.primaryKeys, rowId))
.first(),
await this.dbDriver(parentTn)
.select(parentTable.displayValue.title)
.where(_wherePk(parentTable.primaryKeys, oldRowID))
.first(),
]);
auditUpdateObj.push({
model: auditConfig.parentModel,
childModel: auditConfig.childModel,
rowId: oldRowID as string,
childId: rowId,
op_sub_type: AuditOperationSubTypes.UNLINK_RECORD,
columnTitle: auditConfig.parentColTitle,
pkValue: parentRelatedPkValue,
});
if (parentTable.id !== childTable.id) {
auditUpdateObj.push({
model: auditConfig.childModel,
childModel: auditConfig.parentModel,
rowId: rowId,
childId: oldRowID as string,
op_sub_type: AuditOperationSubTypes.UNLINK_RECORD,
columnTitle: auditConfig.childColTitle,
pkValue: childRelatedPkValue,
});
}
}
await this.execAndParse( await this.execAndParse(
this.dbDriver(childTn) this.dbDriver(childTn)
.update({ .update({
@ -5603,11 +5639,15 @@ class BaseModelSqlv2 {
rowIds: [childId], rowIds: [childId],
cookie, cookie,
}); });
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
} }
break; break;
case RelationTypes.ONE_TO_ONE: case RelationTypes.ONE_TO_ONE:
{ {
const isBt = column.meta?.bt; const isBt = column.meta?.bt;
// todo: unlink if it's already mapped // todo: unlink if it's already mapped
// unlink already mapped record if any // unlink already mapped record if any
await this.execAndParse( await this.execAndParse(
@ -5886,6 +5926,9 @@ class BaseModelSqlv2 {
rowIds: [childId], rowIds: [childId],
cookie, cookie,
}); });
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
} }
break; break;
case RelationTypes.ONE_TO_ONE: case RelationTypes.ONE_TO_ONE:

Loading…
Cancel
Save