Skip to content

Commit

Permalink
fix-1308: Fix hash_from and hash_to is equal in user_contact
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Tremko authored and SamTremko committed Oct 23, 2024
1 parent f563790 commit d51340c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions apps/contact-service/src/db/ContactDbService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,6 +30,10 @@ export interface ContactDbOperations {
hash: HashedPhoneNumber
) => Effect.Effect<void, UnexpectedServerError>

deleteContactsByHashFromAndHashTo: (
args: DeleteContactsByHashFromAndHashToQuery
) => Effect.Effect<void, UnexpectedServerError>

findContactsByHashFrom: (
hash: HashedPhoneNumber
) => Effect.Effect<readonly ContactRecord[], UnexpectedServerError>
Expand Down Expand Up @@ -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* _(
Expand All @@ -75,6 +86,7 @@ export class ContactDbService extends Context.Tag('ContactDbService')<

return {
deleteContactsByHashFrom,
deleteContactsByHashFromAndHashTo,
findContactsByHashFrom,
insertContact,
findFirstLevelContactsPublicKeysByHashFrom,
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
)
}
)
7 changes: 7 additions & 0 deletions apps/contact-service/src/routes/user/updateBadOwnerHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit d51340c

Please sign in to comment.