diff --git a/apps/contact-service/src/db/ContactDbService/index.ts b/apps/contact-service/src/db/ContactDbService/index.ts index afb29d9a0..ecc2df562 100644 --- a/apps/contact-service/src/db/ContactDbService/index.ts +++ b/apps/contact-service/src/db/ContactDbService/index.ts @@ -4,6 +4,10 @@ import {type HashedPhoneNumber} from '@vexl-next/domain/src/general/HashedPhoneN import {Context, Effect, Layer} from 'effect' import {type ContactRecord} from './domain' import {createDeleteContactsByHashFrom} from './queries/createDeleteContactsByHashFrom' +import { + createDeleteContactsByHashFromAndHashTo, + type DeleteContactsByHashFromAndHashToQuery, +} from './queries/createDeleteContactsByHashFromAndHashTo' import { createFindCommonFriendsByOwnerHashAndPublicKeys, type FindCommonFriendsParams, @@ -26,6 +30,10 @@ export interface ContactDbOperations { hash: HashedPhoneNumber ) => Effect.Effect + deleteContactsByHashFromAndHashTo: ( + args: DeleteContactsByHashFromAndHashToQuery + ) => Effect.Effect + findContactsByHashFrom: ( hash: HashedPhoneNumber ) => Effect.Effect @@ -59,6 +67,9 @@ export class ContactDbService extends Context.Tag('ContactDbService')< ContactDbService, Effect.gen(function* (_) { const deleteContactsByHashFrom = yield* _(createDeleteContactsByHashFrom) + const deleteContactsByHashFromAndHashTo = yield* _( + createDeleteContactsByHashFromAndHashTo + ) const findContactsByHashFrom = yield* _(createFindContactsByHashFrom) const insertContact = yield* _(createInsertContact) const findFirstLevelContactsPublicKeysByHashFrom = yield* _( @@ -75,6 +86,7 @@ export class ContactDbService extends Context.Tag('ContactDbService')< return { deleteContactsByHashFrom, + deleteContactsByHashFromAndHashTo, findContactsByHashFrom, insertContact, findFirstLevelContactsPublicKeysByHashFrom, diff --git a/apps/contact-service/src/db/ContactDbService/queries/createDeleteContactsByHashFromAndHashTo.ts b/apps/contact-service/src/db/ContactDbService/queries/createDeleteContactsByHashFromAndHashTo.ts new file mode 100644 index 000000000..f59d976f4 --- /dev/null +++ b/apps/contact-service/src/db/ContactDbService/queries/createDeleteContactsByHashFromAndHashTo.ts @@ -0,0 +1,42 @@ +import {Schema} from '@effect/schema' +import {SqlSchema} from '@effect/sql' +import {PgClient} from '@effect/sql-pg' +import {UnexpectedServerError} from '@vexl-next/domain/src/general/commonErrors' +import {HashedPhoneNumberE} from '@vexl-next/domain/src/general/HashedPhoneNumber.brand' +import {Effect, flow} from 'effect' + +const DeleteContactsByHashFromAndHashToQuery = Schema.Struct({ + hashFrom: HashedPhoneNumberE, + hashTo: HashedPhoneNumberE, +}) + +export type DeleteContactsByHashFromAndHashToQuery = Schema.Schema.Type< + typeof DeleteContactsByHashFromAndHashToQuery +> + +export const createDeleteContactsByHashFromAndHashTo = Effect.gen( + function* (_) { + const sql = yield* _(PgClient.PgClient) + + const query = SqlSchema.void({ + Request: DeleteContactsByHashFromAndHashToQuery, + execute: (hash) => sql` + DELETE FROM user_contact + WHERE + hash_from = ${hash.hashFrom} + AND hash_to = ${hash.hashTo} + `, + }) + + return flow( + query, + Effect.catchAll((e) => + Effect.zipRight( + Effect.logError('Error in deleteContactsByHashFromAndHashTo', e), + Effect.fail(new UnexpectedServerError({status: 500})) + ) + ), + Effect.withSpan('deleteContactsByHashFromAndHashTo query') + ) + } +) diff --git a/apps/contact-service/src/routes/user/updateBadOwnerHash.ts b/apps/contact-service/src/routes/user/updateBadOwnerHash.ts index e18701901..7482d967a 100644 --- a/apps/contact-service/src/routes/user/updateBadOwnerHash.ts +++ b/apps/contact-service/src/routes/user/updateBadOwnerHash.ts @@ -102,6 +102,13 @@ export const updateBadOwnerHash = Handler.make( }) ) + yield* _( + contactDb.deleteContactsByHashFromAndHashTo({ + hashFrom: req.body.newData.hash, + hashTo: req.body.newData.hash, + }) + ) + return { updated: true, }