Browse Source

feat(nocodb): pass prevData to hooks

pull/5349/head
Wing-Kam Wong 2 years ago
parent
commit
ee001d9ac9
  1. 36
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

36
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -1696,15 +1696,17 @@ class BaseModelSqlv2 {
await this.beforeUpdate(data, trx, cookie); await this.beforeUpdate(data, trx, cookie);
const prevData = await this.readByPk(id);
const query = this.dbDriver(this.tnPath) const query = this.dbDriver(this.tnPath)
.update(updateObj) .update(updateObj)
.where(await this._wherePk(id)); .where(await this._wherePk(id));
await this.execAndParse(query); await this.execAndParse(query);
const response = await this.readByPk(id); const newData = await this.readByPk(id);
await this.afterUpdate(response, trx, cookie); await this.afterUpdate(prevData, newData, trx, cookie);
return response; return newData;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
await this.errorUpdate(e, data, trx, cookie); await this.errorUpdate(e, data, trx, cookie);
@ -2122,11 +2124,11 @@ class BaseModelSqlv2 {
* */ * */
public async beforeInsert(data: any, _trx: any, req): Promise<void> { public async beforeInsert(data: any, _trx: any, req): Promise<void> {
await this.handleHooks('Before.insert', data, req); await this.handleHooks('Before.insert', null, data, req);
} }
public async afterInsert(data: any, _trx: any, req): Promise<void> { public async afterInsert(data: any, _trx: any, req): Promise<void> {
await this.handleHooks('After.insert', data, req); await this.handleHooks('After.insert', null, data, req);
// if (req?.headers?.['xc-gui']) { // if (req?.headers?.['xc-gui']) {
const id = this._extractPksValues(data); const id = this._extractPksValues(data);
await Audit.insert({ await Audit.insert({
@ -2194,12 +2196,18 @@ class BaseModelSqlv2 {
} }
} }
if (ignoreWebhook === undefined || ignoreWebhook === 'false') { if (ignoreWebhook === undefined || ignoreWebhook === 'false') {
await this.handleHooks('Before.update', data, req); await this.handleHooks('Before.update', null, data, req);
} }
} }
public async afterUpdate(data: any, _trx: any, req): Promise<void> { public async afterUpdate(
const id = this._extractPksValues(data); prevData: any,
newData: any,
_trx: any,
req
): Promise<void> {
const id = this._extractPksValues(newData);
await Audit.insert({ await Audit.insert({
fk_model_id: this.model.id, fk_model_id: this.model.id,
row_id: id, row_id: id,
@ -2218,12 +2226,12 @@ class BaseModelSqlv2 {
} }
} }
if (ignoreWebhook === undefined || ignoreWebhook === 'false') { if (ignoreWebhook === undefined || ignoreWebhook === 'false') {
await this.handleHooks('After.update', data, req); await this.handleHooks('After.update', prevData, newData, req);
} }
} }
public async beforeDelete(data: any, _trx: any, req): Promise<void> { public async beforeDelete(data: any, _trx: any, req): Promise<void> {
await this.handleHooks('Before.delete', data, req); await this.handleHooks('Before.delete', null, data, req);
} }
public async afterDelete(data: any, _trx: any, req): Promise<void> { public async afterDelete(data: any, _trx: any, req): Promise<void> {
@ -2240,10 +2248,10 @@ class BaseModelSqlv2 {
user: req?.user?.email, user: req?.user?.email,
}); });
// } // }
await this.handleHooks('After.delete', data, req); await this.handleHooks('After.delete', null, data, req);
} }
private async handleHooks(hookName, data, req): Promise<void> { private async handleHooks(hookName, prevData, newData, req): Promise<void> {
const view = await View.get(this.viewId); const view = await View.get(this.viewId);
// handle form view data submission // handle form view data submission
@ -2297,7 +2305,7 @@ class BaseModelSqlv2 {
.map((a) => a[0]); .map((a) => a[0]);
if (emails?.length) { if (emails?.length) {
const transformedData = _transformSubmittedFormDataForEmail( const transformedData = _transformSubmittedFormDataForEmail(
data, newData,
formView, formView,
filteredColumns filteredColumns
); );
@ -2325,7 +2333,7 @@ class BaseModelSqlv2 {
}); });
for (const hook of hooks) { for (const hook of hooks) {
if (hook.active) { if (hook.active) {
invokeWebhook(hook, this.model, view, data, req?.user); invokeWebhook(hook, this.model, view, prevData, newData, req?.user);
} }
} }
} catch (e) { } catch (e) {

Loading…
Cancel
Save