|
| 1 | +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; |
| 2 | +import bitport from "../../bitport.app.mjs"; |
| 3 | +import { prepareList } from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + props: { |
| 7 | + bitport, |
| 8 | + db: "$.service.db", |
| 9 | + timer: { |
| 10 | + type: "$.interface.timer", |
| 11 | + default: { |
| 12 | + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, |
| 13 | + }, |
| 14 | + }, |
| 15 | + folderCode: { |
| 16 | + propDefinition: [ |
| 17 | + bitport, |
| 18 | + "folderCode", |
| 19 | + ], |
| 20 | + withLabel: true, |
| 21 | + }, |
| 22 | + }, |
| 23 | + methods: { |
| 24 | + _getLastDate() { |
| 25 | + return this.db.get("lastDate") || 0; |
| 26 | + }, |
| 27 | + _setLastDate(lastDate) { |
| 28 | + this.db.set("lastDate", lastDate); |
| 29 | + }, |
| 30 | + getFilter() { |
| 31 | + return false; |
| 32 | + }, |
| 33 | + async emitEvent(maxResults = false) { |
| 34 | + const lastDate = this._getLastDate(); |
| 35 | + |
| 36 | + const bufferObj = Buffer.from(this.folderCode.label, "utf8"); |
| 37 | + const base64String = bufferObj.toString("base64"); |
| 38 | + |
| 39 | + const { data: response } = await this.bitport.listFolders({ |
| 40 | + maxResults, |
| 41 | + params: { |
| 42 | + folderPath: base64String, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + let items = prepareList({ |
| 47 | + items: response, |
| 48 | + filesOnly: true, |
| 49 | + }); |
| 50 | + |
| 51 | + if (items.length) { |
| 52 | + items = items.filter((item) => { |
| 53 | + return Date.parse(item.created_at.date) > lastDate; |
| 54 | + }) |
| 55 | + .sort((a, b) => Date.parse(b.created_at.date) - Date.parse(a.created_at.date)); |
| 56 | + |
| 57 | + const filteredItems = this.getFilter(items); |
| 58 | + if (filteredItems) { |
| 59 | + items = filteredItems; |
| 60 | + } |
| 61 | + if (items.length) { |
| 62 | + if (maxResults && (items.length > maxResults)) { |
| 63 | + items.length = maxResults; |
| 64 | + } |
| 65 | + this._setLastDate(Date.parse(items[0].created_at.date)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for (const item of items.reverse()) { |
| 70 | + this.$emit(item, { |
| 71 | + id: item.code, |
| 72 | + summary: this.getSummary(item), |
| 73 | + ts: Date.parse(item.created_at.date), |
| 74 | + }); |
| 75 | + } |
| 76 | + }, |
| 77 | + }, |
| 78 | + hooks: { |
| 79 | + async deploy() { |
| 80 | + await this.emitEvent(25); |
| 81 | + }, |
| 82 | + }, |
| 83 | + async run() { |
| 84 | + await this.emitEvent(); |
| 85 | + }, |
| 86 | +}; |
0 commit comments