Skip to content

Commit

Permalink
fix: updating flagged offers should not fail
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladivo committed Sep 17, 2024
1 parent 4248d6b commit c0abdd6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
53 changes: 53 additions & 0 deletions apps/offer-service/src/__tests__/utils/routes/updateOffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
])
)
)}
Expand Down
1 change: 1 addition & 0 deletions apps/offer-service/src/routes/updateOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () =>
Expand Down

0 comments on commit c0abdd6

Please sign in to comment.