From c0abdd6a2bac0c7bf5b168cf04a851bef9b59ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Nov=C3=A1k?= Date: Tue, 20 Aug 2024 10:40:21 +0200 Subject: [PATCH] fix: updating flagged offers should not fail --- .../utils/routes/updateOffer.test.ts | 53 +++++++++++++++++++ .../createQueryOfferByPublicKeyAndOfferId.ts | 9 +++- apps/offer-service/src/routes/updateOffer.ts | 1 + 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/apps/offer-service/src/__tests__/utils/routes/updateOffer.test.ts b/apps/offer-service/src/__tests__/utils/routes/updateOffer.test.ts index 5171023e4..c0d4894ac 100644 --- a/apps/offer-service/src/__tests__/utils/routes/updateOffer.test.ts +++ b/apps/offer-service/src/__tests__/utils/routes/updateOffer.test.ts @@ -346,4 +346,57 @@ describe('Update offer', () => { }) ) }) + + it('Does not fail when updating expired or flagged offer', async () => { + await runPromiseInMockedEnvironment( + Effect.gen(function* (_) { + const client = yield* _(NodeTestingApp) + const sql = yield* _(SqlClient.SqlClient) + + yield* _(sql` + UPDATE offer_public + SET + refreshed_at = NOW() - INTERVAL '8 days', + report = 3 + WHERE + offer_id = ${offer1.offerId} + `) + + const result = yield* _( + client.updateOffer( + { + body: { + adminId: offer1.adminId, + payloadPublic: Schema.decodeUnknownSync( + PublicPayloadEncryptedE + )('newPayloadPublic'), + offerPrivateList: [], + }, + }, + HttpClientRequest.setHeaders( + yield* _( + createDummyAuthHeadersForUser({ + phoneNumber: + Schema.decodeSync(E164PhoneNumberE)('+420733333333'), + publicKey: me.publicKeyPemBase64, + }) + ) + ) + ), + Effect.either + ) + + yield* _(sql` + UPDATE offer_public + SET + refreshed_at = NOW(), + report = 0 + WHERE + offer_id = ${offer1.offerId} + `) + + expect(result._tag).toBe('Right') + }) + ) + }) }) diff --git a/apps/offer-service/src/db/OfferDbService/queries/createQueryOfferByPublicKeyAndOfferId.ts b/apps/offer-service/src/db/OfferDbService/queries/createQueryOfferByPublicKeyAndOfferId.ts index 4b2762772..1312764bc 100644 --- a/apps/offer-service/src/db/OfferDbService/queries/createQueryOfferByPublicKeyAndOfferId.ts +++ b/apps/offer-service/src/db/OfferDbService/queries/createQueryOfferByPublicKeyAndOfferId.ts @@ -19,6 +19,7 @@ import { export const QueryOfferByPublicKeyAndOfferIdRequest = Schema.Struct({ userPublicKey: PublicKeyPemBase64E, id: OfferIdE, + skipValidation: Schema.optional(Schema.Boolean), }) export type QueryOfferByPublicKeyAndOfferIdRequest = Schema.Schema.Type< typeof QueryOfferByPublicKeyAndOfferIdRequest @@ -49,8 +50,12 @@ export const createQueryOfferByPublicKeyAndOfferId = Effect.gen(function* (_) { sql.and([ sql`offer_public.offer_id = ${one.id}`, sql`offer_private.user_public_key = ${one.userPublicKey}`, - offerNotExpired(sql, expirationPeriodDays), - offerNotFlagged(sql, offerReportFilter), + one.skipValidation + ? 'true' + : offerNotExpired(sql, expirationPeriodDays), + one.skipValidation + ? 'true' + : offerNotFlagged(sql, offerReportFilter), ]) ) )} diff --git a/apps/offer-service/src/routes/updateOffer.ts b/apps/offer-service/src/routes/updateOffer.ts index 97fe1e16a..9b0021c40 100644 --- a/apps/offer-service/src/routes/updateOffer.ts +++ b/apps/offer-service/src/routes/updateOffer.ts @@ -65,6 +65,7 @@ export const updateOffer = Handler.make(UpdateOfferEndpoint, (req, security) => offerDb.queryOfferByPublicKeyAndOfferId({ id: publicPartFromDb.offerId, userPublicKey: security['public-key'], + skipValidation: true, }), Effect.flatten, Effect.catchTag('NoSuchElementException', () =>