|
| 1 | +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; |
| 2 | +import wati from "../../wati.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + props: { |
| 6 | + wati, |
| 7 | + db: "$.service.db", |
| 8 | + timer: { |
| 9 | + type: "$.interface.timer", |
| 10 | + default: { |
| 11 | + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, |
| 12 | + }, |
| 13 | + }, |
| 14 | + }, |
| 15 | + methods: { |
| 16 | + prepareData(data) { |
| 17 | + return data; |
| 18 | + }, |
| 19 | + _getLastDate() { |
| 20 | + return this.db.get("lastDate") || 0; |
| 21 | + }, |
| 22 | + _setLastDate(lastDate) { |
| 23 | + this.db.set("lastDate", lastDate); |
| 24 | + }, |
| 25 | + async emitEvent(maxResults = false) { |
| 26 | + const lastDate = this._getLastDate(); |
| 27 | + const dateField = this.getDateField(); |
| 28 | + |
| 29 | + const response = this.wati.paginate( |
| 30 | + this.getPaginateOpts(maxResults), |
| 31 | + ); |
| 32 | + |
| 33 | + let responseArray = []; |
| 34 | + for await (const item of response) { |
| 35 | + responseArray.push(item); |
| 36 | + } |
| 37 | + |
| 38 | + responseArray = this.prepareData(responseArray, lastDate, maxResults); |
| 39 | + |
| 40 | + if (responseArray.length) { |
| 41 | + this._setLastDate(responseArray[0][dateField]); |
| 42 | + } |
| 43 | + |
| 44 | + for (const item of responseArray.reverse()) { |
| 45 | + this.$emit(item, { |
| 46 | + id: item.id, |
| 47 | + summary: this.getSummary(item), |
| 48 | + ts: Date.parse(item[dateField]), |
| 49 | + }); |
| 50 | + } |
| 51 | + }, |
| 52 | + }, |
| 53 | + hooks: { |
| 54 | + async deploy() { |
| 55 | + await this.emitEvent(25); |
| 56 | + }, |
| 57 | + }, |
| 58 | + async run() { |
| 59 | + await this.emitEvent(); |
| 60 | + }, |
| 61 | +}; |
0 commit comments