|
| 1 | +import { v4 as uuid } from 'uuid' |
| 2 | +import { QueryTypes } from 'sequelize' |
| 3 | +import { |
| 4 | + DbIncomingWebhookInsertData, |
| 5 | + IncomingWebhookData, |
| 6 | + WebhookState, |
| 7 | +} from '../../types/webhooks' |
| 8 | +import { IRepositoryOptions } from './IRepositoryOptions' |
| 9 | +import { RepositoryBase } from './repositoryBase' |
| 10 | + |
| 11 | +/* eslint-disable class-methods-use-this */ |
| 12 | + |
| 13 | +export default class IncomingWebhookRepository extends RepositoryBase< |
| 14 | + IncomingWebhookData, |
| 15 | + string, |
| 16 | + DbIncomingWebhookInsertData, |
| 17 | + unknown, |
| 18 | + unknown |
| 19 | +> { |
| 20 | + public constructor(options: IRepositoryOptions) { |
| 21 | + super(options, true) |
| 22 | + } |
| 23 | + |
| 24 | + async create(data: DbIncomingWebhookInsertData): Promise<IncomingWebhookData> { |
| 25 | + const transaction = this.transaction |
| 26 | + |
| 27 | + const id = uuid() |
| 28 | + |
| 29 | + const results = await this.seq.query( |
| 30 | + ` |
| 31 | + insert into "incomingWebhooks"(id, "tenantId", "integrationId", state, type, payload) |
| 32 | + values(:id, :tenantId, :integrationId, :state, :type, :payload) |
| 33 | + returning "createdAt" |
| 34 | + `, |
| 35 | + { |
| 36 | + replacements: { |
| 37 | + id, |
| 38 | + tenantId: data.tenantId, |
| 39 | + integrationId: data.integrationId, |
| 40 | + type: data.type, |
| 41 | + state: WebhookState.PENDING, |
| 42 | + payload: JSON.stringify(data.payload), |
| 43 | + }, |
| 44 | + type: QueryTypes.INSERT, |
| 45 | + transaction, |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + return { |
| 50 | + id, |
| 51 | + state: WebhookState.PENDING, |
| 52 | + ...data, |
| 53 | + processedAt: null, |
| 54 | + error: null, |
| 55 | + createdAt: results[0][0].createdAt.toISOString(), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override async findById(id: string): Promise<IncomingWebhookData> { |
| 60 | + const transaction = this.transaction |
| 61 | + |
| 62 | + const seq = this.seq |
| 63 | + |
| 64 | + const results = await seq.query( |
| 65 | + ` |
| 66 | + select id, |
| 67 | + "tenantId", |
| 68 | + "integrationId", |
| 69 | + state, |
| 70 | + type, |
| 71 | + payload, |
| 72 | + "processedAt", |
| 73 | + error, |
| 74 | + "createdAt" |
| 75 | + from "incomingWebhooks" |
| 76 | + where id = :id |
| 77 | + `, |
| 78 | + { |
| 79 | + replacements: { |
| 80 | + id, |
| 81 | + }, |
| 82 | + type: QueryTypes.SELECT, |
| 83 | + transaction, |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | + if (results.length === 0) { |
| 88 | + return null |
| 89 | + } |
| 90 | + |
| 91 | + const data = results[0] as any |
| 92 | + |
| 93 | + return { |
| 94 | + id: data.id, |
| 95 | + tenantId: data.tenantId, |
| 96 | + integrationId: data.integrationId, |
| 97 | + state: data.state, |
| 98 | + type: data.type, |
| 99 | + payload: data.payload, |
| 100 | + processedAt: data.processedAt ? data.processedAt.toISOString() : null, |
| 101 | + error: data.error, |
| 102 | + createdAt: data.createdAt.toISOString(), |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + async markCompleted(id: string): Promise<void> { |
| 107 | + const transaction = this.transaction |
| 108 | + |
| 109 | + const [, rowCount] = await this.seq.query( |
| 110 | + ` |
| 111 | + update "incomingWebhooks" |
| 112 | + set state = :state, |
| 113 | + "processedAt" = now() |
| 114 | + where id = :id |
| 115 | + `, |
| 116 | + { |
| 117 | + replacements: { |
| 118 | + id, |
| 119 | + state: WebhookState.PROCESSED, |
| 120 | + }, |
| 121 | + type: QueryTypes.UPDATE, |
| 122 | + transaction, |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + if (rowCount !== 1) { |
| 127 | + throw new Error(`Failed to mark webhook '${id}' as completed!`) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + async markError(id: string, error: any): Promise<void> { |
| 132 | + const transaction = this.transaction |
| 133 | + |
| 134 | + const [, rowCount] = await this.seq.query( |
| 135 | + ` |
| 136 | + update "incomingWebhooks" |
| 137 | + set state = :state, |
| 138 | + error = :error |
| 139 | + where id = :id |
| 140 | + `, |
| 141 | + { |
| 142 | + replacements: { |
| 143 | + id, |
| 144 | + state: WebhookState.ERROR, |
| 145 | + error: JSON.stringify(error), |
| 146 | + }, |
| 147 | + type: QueryTypes.UPDATE, |
| 148 | + transaction, |
| 149 | + }, |
| 150 | + ) |
| 151 | + |
| 152 | + if (rowCount !== 1) { |
| 153 | + throw new Error(`Failed to mark webhook '${id}' as error!`) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments