| 76 | } |
| 77 | |
| 78 | public upsert(invoice: Invoice, client: DatabaseClient = this.dbClient): Promise<number> { |
| 79 | logger('upserting invoice: %o', invoice) |
| 80 | |
| 81 | const row = applySpec<DBInvoice>({ |
| 82 | id: ifElse(propSatisfies(is(String), 'id'), prop('id'), always(randomUUID())), |
| 83 | pubkey: pipe(prop('pubkey'), toBuffer), |
| 84 | bolt11: prop('bolt11'), |
| 85 | amount_requested: pipe(prop('amountRequested'), toString), |
| 86 | // amount_paid: ifElse(propSatisfies(is(BigInt), 'amountPaid'), pipe(prop('amountPaid'), toString), always(null)), |
| 87 | unit: prop('unit'), |
| 88 | status: prop('status'), |
| 89 | description: prop('description'), |
| 90 | // confirmed_at: prop('confirmedAt'), |
| 91 | expires_at: prop('expiresAt'), |
| 92 | updated_at: always(new Date()), |
| 93 | created_at: prop('createdAt'), |
| 94 | verify_url: prop('verifyURL'), |
| 95 | })(invoice) |
| 96 | |
| 97 | logger('row: %o', row) |
| 98 | |
| 99 | const query = client<DBInvoice>('invoices') |
| 100 | .insert(row) |
| 101 | .onConflict('id') |
| 102 | .merge( |
| 103 | omit([ |
| 104 | 'id', |
| 105 | 'pubkey', |
| 106 | 'bolt11', |
| 107 | 'amount_requested', |
| 108 | 'unit', |
| 109 | 'description', |
| 110 | 'expires_at', |
| 111 | 'created_at', |
| 112 | 'verify_url', |
| 113 | ])(row), |
| 114 | ) |
| 115 | |
| 116 | return { |
| 117 | then: <T1, T2>( |
| 118 | onfulfilled: (value: number) => T1 | PromiseLike<T1>, |
| 119 | onrejected: (reason: any) => T2 | PromiseLike<T2>, |
| 120 | ) => query.then(prop('rowCount') as () => number).then(onfulfilled, onrejected), |
| 121 | catch: <T>(onrejected: (reason: any) => T | PromiseLike<T>) => query.catch(onrejected), |
| 122 | toString: (): string => query.toString(), |
| 123 | } as Promise<number> |
| 124 | } |
| 125 | } |