import base from "../common/common.mjs";
import moment from "moment";
export default {
...base,
name: "New or Modified Records in View",
description: "Emit new event for each new or modified record in a view",
key: "airtable_oauth-new-or-modified-records-in-view",
version: "0.0.7",
type: "source",
props: {
...base.props,
tableId: {
propDefinition: [
base.props.airtable,
"tableId",
({ baseId }) => ({
baseId,
}),
],
description: "The table ID to watch for changes.",
},
viewId: {
propDefinition: [
base.props.airtable,
"viewId",
({
baseId, tableId,
}) => ({
baseId,
tableId,
}),
],
description: "The view ID to watch for changes.",
},
returnFieldsByFieldId: {
propDefinition: [
base.props.airtable,
"returnFieldsByFieldId",
],
},
},
async run(event) {
const {
baseId,
tableId,
viewId,
} = this;
const lastTimestamp = this._getLastTimestamp();
const params = {
view: viewId,
filterByFormula: `LAST_MODIFIED_TIME() > "${lastTimestamp}"`,
returnFieldsByFieldId: this.returnFieldsByFieldId || false,
};
const records = await this.airtable.listRecords({
baseId,
tableId,
params,
});
if (!records.length) {
console.log("No new or modified records.");
return;
}
const metadata = {
baseId,
tableId,
viewId,
};
let newRecords = 0, modifiedRecords = 0;
for (const record of records) {
if (!lastTimestamp || moment(record.createdTime) > moment(lastTimestamp)) {
record.type = "new_record";
newRecords++;
} else {
record.type = "record_modified";
modifiedRecords++;
}
record.metadata = metadata;
this.$emit(record, {
summary: `${record.type}: ${JSON.stringify(record.fields)}`,
id: record.id,
});
}
console.log(`Emitted ${newRecords} new records(s) and ${modifiedRecords} modified record(s).`);
this.updateLastTimestamp(event);
},
};