From 0b65ec502b7241dff485153f0b03db300ff787e2 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Thu, 25 Apr 2024 21:22:43 +0200 Subject: [PATCH] fix(api): /charge-stripe-card responses (#54530) Co-authored-by: Naomi --- api/src/routes/donate.test.ts | 24 ++++++---- api/src/routes/donate.ts | 46 +++++++++++--------- api/src/schemas/donate/charge-stripe-card.ts | 21 +++++---- 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/api/src/routes/donate.test.ts b/api/src/routes/donate.test.ts index 3c489aa015d..ff1a35f15d4 100644 --- a/api/src/routes/donate.test.ts +++ b/api/src/routes/donate.test.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/naming-convention */ import { createSuperRequest, devLogin, @@ -77,9 +76,11 @@ describe('Donate', () => { ); expect(response.body).toEqual({ - type: 'UserActionRequired', - message: 'Payment requires user action', - client_secret: 'superSecret' + error: { + type: 'UserActionRequired', + message: 'Payment requires user action', + client_secret: 'superSecret' + } }); expect(response.status).toBe(402); }); @@ -93,8 +94,10 @@ describe('Donate', () => { ); expect(response.body).toEqual({ - type: 'PaymentMethodRequired', - message: 'Card has been declined' + error: { + type: 'PaymentMethodRequired', + message: 'Card has been declined' + } }); expect(response.status).toBe(402); }); @@ -111,6 +114,12 @@ describe('Donate', () => { const failResponse = await superPost('/donate/charge-stripe-card').send( chargeStripeCardReqBody ); + expect(failResponse.body).toEqual({ + error: { + type: 'AlreadyDonatingError', + message: 'User is already donating.' + } + }); expect(failResponse.status).toBe(400); }); @@ -121,8 +130,7 @@ describe('Donate', () => { ); expect(response.status).toBe(500); expect(response.body).toEqual({ - type: 'danger', - message: 'Donation failed due to a server error.' + error: 'Donation failed due to a server error.' }); }); }); diff --git a/api/src/routes/donate.ts b/api/src/routes/donate.ts index d479c8da621..e7ba329754b 100644 --- a/api/src/routes/donate.ts +++ b/api/src/routes/donate.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/naming-convention */ import { Type, type FastifyPluginCallbackTypebox @@ -106,10 +105,12 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( if (user.isDonating) { void reply.code(400); - return { - type: 'info', - message: 'User is already donating.' - } as const; + return reply.send({ + error: { + type: 'AlreadyDonatingError', + message: 'User is already donating.' + } + }); } // Create Stripe Customer @@ -141,18 +142,22 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( }); if (status === 'requires_source_action') { void reply.code(402); - return { - type: 'UserActionRequired', - message: 'Payment requires user action', - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - client_secret - } as const; + return reply.send({ + error: { + type: 'UserActionRequired', + message: 'Payment requires user action', + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + client_secret + } + }); } else if (status === 'requires_source') { void reply.code(402); - return { - type: 'PaymentMethodRequired', - message: 'Card has been declined' - } as const; + return reply.send({ + error: { + type: 'PaymentMethodRequired', + message: 'Card has been declined' + } + }); } // update record in database @@ -182,17 +187,16 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( } }); - return { + return reply.send({ type: 'success', isDonating: true - } as const; + }); } catch (error) { fastify.log.error(error); void reply.code(500); - return { - type: 'danger', - message: 'Donation failed due to a server error.' - } as const; + return reply.send({ + error: 'Donation failed due to a server error.' + }); } } ); diff --git a/api/src/schemas/donate/charge-stripe-card.ts b/api/src/schemas/donate/charge-stripe-card.ts index 6726ca632cf..50b21148bba 100644 --- a/api/src/schemas/donate/charge-stripe-card.ts +++ b/api/src/schemas/donate/charge-stripe-card.ts @@ -12,18 +12,23 @@ export const chargeStripeCard = { type: Type.Literal('success') }), 400: Type.Object({ - message: Type.String(), - type: Type.Literal('info') + error: Type.Object({ + message: Type.String(), + type: Type.Literal('AlreadyDonatingError') + }) }), 402: Type.Object({ - message: Type.String(), - type: Type.String(), - // eslint-disable-next-line @typescript-eslint/naming-convention - client_secret: Type.Optional(Type.String()) + error: Type.Object({ + message: Type.String(), + type: Type.Union([ + Type.Literal('UserActionRequired'), + Type.Literal('PaymentMethodRequired') + ]), + client_secret: Type.Optional(Type.String()) + }) }), 500: Type.Object({ - message: Type.String(), - type: Type.Literal('danger') + error: Type.Literal('Donation failed due to a server error.') }) } };