Skip to content

Commit eaa6100

Browse files
committed
Adjusting event format for record-based sources
1 parent b3e8b11 commit eaa6100

File tree

5 files changed

+73
-43
lines changed

5 files changed

+73
-43
lines changed

components/airtable_oauth/sources/common/common-webhook-field.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import airtable from "../../airtable_oauth.app.mjs";
21
import common from "./common-webhook.mjs";
32

43
export default {
@@ -21,7 +20,7 @@ export default {
2120
this.db.set("tableSchemas", filteredData);
2221
}
2322
},
24-
async emitEvent(payload) {
23+
emitEvent(payload) {
2524
const [tableId, tableData] = Object.entries(payload.changedTablesById)[0];
2625
const [operation, fieldObj] = Object.entries(tableData)[0];
2726
const [fieldId, fieldUpdateInfo] = Object.entries(fieldObj)[0];
Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import airtable from "../../airtable_oauth.app.mjs";
1+
22
import common from "./common-webhook.mjs";
33

44
export default {
@@ -10,42 +10,40 @@ export default {
1010
"tableData",
1111
];
1212
},
13-
},
14-
props: {
15-
...common.props,
16-
watchDataInFieldIds: {
17-
propDefinition: [
18-
airtable,
19-
"sortFieldId",
20-
(c) => ({
21-
baseId: c.baseId,
22-
tableId: c.tableId,
23-
}),
24-
],
25-
type: "string[]",
26-
label: "Watch Data In Field Ids",
27-
description:
28-
"Only emit events for updates that modify values in cells in these fields. If omitted, all fields within the table/view/base are watched",
29-
},
30-
includeCellValuesInFieldIds: {
31-
propDefinition: [
32-
airtable,
33-
"sortFieldId",
34-
(c) => ({
35-
baseId: c.baseId,
36-
tableId: c.tableId,
37-
}),
38-
],
39-
type: "string[]",
40-
label: "Include Cell Values in Field Ids",
41-
description: "Fields to include in the event payload, regardless of whether or not they changed",
42-
},
43-
includePreviousCellValues: {
44-
type: "boolean",
45-
label: "Include Previous Cell Values",
46-
description:
47-
"If true, include the previous cell value in the event payload",
48-
optional: true,
13+
async emitEvent(payload) {
14+
const [tableId, tableData] = Object.entries(payload.changedTablesById)[0];
15+
let [operation, recordObj] = Object.entries(tableData)[0];
16+
if (operation === 'changedViewsById') {
17+
const changedRecord = Object.entries(recordObj)[0];
18+
operation = changedRecord[0];
19+
recordObj = changedRecord[1];
20+
}
21+
const [recordId, recordUpdateInfo] = Object.entries(recordObj)[0];
22+
23+
let updateType = 'updated';
24+
if (operation === "createdRecordsById") {
25+
updateType = "created";
26+
} else if (operation === "deletedRecordsById") {
27+
updateType = "deleted";
28+
}
29+
30+
const { fields } = await this.airtable.getRecord({
31+
baseId: this.baseId,
32+
tableId,
33+
recordId
34+
});
35+
36+
const summary = `Record ${updateType}: ${fields?.name ?? recordId}`
37+
38+
this.$emit({
39+
originalPayload: payload,
40+
tableId,
41+
record: {
42+
id: recordId,
43+
fields,
44+
},
45+
recordUpdateInfo,
46+
}, this.generateMeta(payload, summary));
4947
},
5048
},
5149
};

components/airtable_oauth/sources/common/common-webhook.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ export default {
114114
const meta = this.generateMeta(payload);
115115
this.$emit(payload, meta);
116116
},
117-
emitEvent(payload) {
117+
async emitEvent(payload) {
118118
// sources may call this to customize event emission, but it is
119119
// a separate method so that the source may fall back to default
120-
this.emitDefaultEvent(payload);
120+
return this.emitDefaultEvent(payload);
121121
},
122122
async saveAdditionalData() {
123123
// sources may call this to fetch additional data (e.g. field schemas)
@@ -149,7 +149,7 @@ export default {
149149
});
150150
for (const payload of payloads) {
151151
try {
152-
this.emitEvent(payload);
152+
await this.emitEvent(payload);
153153
} catch (err) {
154154
console.log('Error emitting event, defaulting to default emission');
155155
console.log(err);

components/airtable_oauth/sources/new-modified-or-deleted-records-instant/new-modified-or-deleted-records-instant.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import common from "../common/common-webhook-record.mjs";
22
import constants from "../common/constants.mjs";
33
import sampleEmit from "./test-event.mjs";
4+
import airtable from "../../airtable_oauth.app.mjs";
45

56
export default {
67
...common,
@@ -19,6 +20,20 @@ export default {
1920
options: constants.CHANGE_TYPES,
2021
optional: true,
2122
},
23+
watchDataInFieldIds: {
24+
propDefinition: [
25+
airtable,
26+
"sortFieldId",
27+
(c) => ({
28+
baseId: c.baseId,
29+
tableId: c.tableId,
30+
}),
31+
],
32+
type: "string[]",
33+
label: "Watch Data In Field Ids",
34+
description:
35+
"Only emit events for updates that modify values in cells in these fields. If omitted, all fields within the table/view/base are watched",
36+
},
2237
},
2338
methods: {
2439
...common.methods,

components/airtable_oauth/sources/new-or-modified-records/new-or-modified-records.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import common from "../common/common-webhook-record.mjs";
2+
import airtable from "../../airtable_oauth.app.mjs";
23

34
export default {
45
...common,
56
name: "New or Modified Records (Instant)",
67
key: "airtable_oauth-new-or-modified-records",
78
description: "Emit new event for each new or modified record in a table or view",
8-
version: "1.0.0",
9+
version: "1.0.{{ts}}",
910
type: "source",
1011
dedupe: "unique",
1112
methods: {
@@ -17,4 +18,21 @@ export default {
1718
];
1819
},
1920
},
21+
props: {
22+
...common.props,
23+
watchDataInFieldIds: {
24+
propDefinition: [
25+
airtable,
26+
"sortFieldId",
27+
(c) => ({
28+
baseId: c.baseId,
29+
tableId: c.tableId,
30+
}),
31+
],
32+
type: "string[]",
33+
label: "Watch Data In Field Ids",
34+
description:
35+
"Only emit events for updates that modify values in cells in these fields. If omitted, all fields within the table/view/base are watched",
36+
},
37+
},
2038
};

0 commit comments

Comments
 (0)