|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import drimify from "../../drimify.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "drimify-new-application-data", |
| 6 | + name: "New Application Data Collected", |
| 7 | + description: "Emit new event when application data has been collected. [See the documentation](https://endpoint.drimify.com/api/docs?ui=re_doc)", |
| 8 | + version: "0.0.{{ts}}", |
| 9 | + type: "source", |
| 10 | + dedupe: "unique", |
| 11 | + props: { |
| 12 | + drimify, |
| 13 | + db: "$.service.db", |
| 14 | + timer: { |
| 15 | + type: "$.interface.timer", |
| 16 | + default: { |
| 17 | + intervalSeconds: 60 * 15, |
| 18 | + }, |
| 19 | + }, |
| 20 | + applicationId: { |
| 21 | + propDefinition: [ |
| 22 | + drimify, |
| 23 | + "applicationId", |
| 24 | + ], |
| 25 | + }, |
| 26 | + timeFrame: { |
| 27 | + propDefinition: [ |
| 28 | + drimify, |
| 29 | + "timeFrame", |
| 30 | + ], |
| 31 | + optional: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + hooks: { |
| 35 | + async deploy() { |
| 36 | + const applicationData = await this.drimify.paginate(this.drimify.listAppDataCollections, { |
| 37 | + applicationId: this.applicationId, |
| 38 | + createdSince: this.timeFrame || undefined, |
| 39 | + }); |
| 40 | + |
| 41 | + for (const data of applicationData.slice(0, 50).reverse()) { |
| 42 | + this.$emit(data, { |
| 43 | + id: data.idunic, |
| 44 | + summary: `New Data Collected: ${data.username || data.email || data.idunic}`, |
| 45 | + ts: new Date(data.updatedAt).getTime(), |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const lastTimestamp = applicationData[0]?.updatedAtTimestamp || 0; |
| 50 | + this._setLastTimestamp(lastTimestamp); |
| 51 | + }, |
| 52 | + async activate() { |
| 53 | + // Implement if needed |
| 54 | + }, |
| 55 | + async deactivate() { |
| 56 | + // Implement if needed |
| 57 | + }, |
| 58 | + }, |
| 59 | + methods: { |
| 60 | + _getLastTimestamp() { |
| 61 | + return this.db.get("lastTimestamp") || 0; |
| 62 | + }, |
| 63 | + _setLastTimestamp(timestamp) { |
| 64 | + this.db.set("lastTimestamp", timestamp); |
| 65 | + }, |
| 66 | + }, |
| 67 | + async run() { |
| 68 | + const lastTimestamp = this._getLastTimestamp(); |
| 69 | + const applicationData = await this.drimify.paginate(this.drimify.listAppDataCollections, { |
| 70 | + applicationId: this.applicationId, |
| 71 | + createdSince: new Date(lastTimestamp * 1000).toISOString(), |
| 72 | + }); |
| 73 | + |
| 74 | + for (const data of applicationData) { |
| 75 | + if (Date.parse(data.updatedAt) > lastTimestamp) { |
| 76 | + this.$emit(data, { |
| 77 | + id: data.idunic, |
| 78 | + summary: `New Data Collected: ${data.username || data.email || data.idunic}`, |
| 79 | + ts: new Date(data.updatedAt).getTime(), |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (applicationData.length > 0) { |
| 85 | + this._setLastTimestamp(applicationData[0].updatedAtTimestamp || 0); |
| 86 | + } |
| 87 | + }, |
| 88 | +}; |
0 commit comments