Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-1308: Fix hash_from and hash_to is equal in user_contact #1337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading