Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 19 additions & 7 deletions backend/src/services/member/memberIdentityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import lodash from 'lodash'

import { captureApiChange, memberEditIdentitiesAction } from '@crowd/audit-logs'
import { Error409 } from '@crowd/common'
import { Error404, Error409 } from '@crowd/common'
import { createMemberIdentity, findIdentitiesForMembers, optionsQx } from '@crowd/data-access-layer'
import {
checkMemberIdentityExistence,
Expand Down Expand Up @@ -62,7 +62,9 @@ export default class MemberIdentityService extends LoggerBase {
qx,
data.value,
data.platform,
data.type,
)

if (existingIdentities.length > 0) {
Comment thread
Copilot marked this conversation as resolved.
Outdated
throw new Error409(
this.options.language,
Expand Down Expand Up @@ -134,6 +136,7 @@ export default class MemberIdentityService extends LoggerBase {
qx,
identity.value,
identity.platform,
identity.type,
)

if (existingIdentities.length > 0) {
Expand Down Expand Up @@ -203,20 +206,29 @@ export default class MemberIdentityService extends LoggerBase {

const qx = SequelizeRepository.getQueryExecutor(repoOptions)

// Check if identity already exists
const currentIdentity = memberIdentities.find((identity) => identity.id === id)
if (!currentIdentity) {
throw new Error404(this.options.language, 'errors.notFound.message')
}

const value = data.value ?? currentIdentity.value
const platform = data.platform ?? currentIdentity.platform
const type = data.type ?? currentIdentity.type

const existingIdentities = await checkMemberIdentityExistence(
qx,
data.value,
data.platform,
value,
platform,
type,
)
const filteredExistingIdentities = existingIdentities.filter((i) => i.id !== id)
if (filteredExistingIdentities.length > 0) {
const conflict = existingIdentities.find((identity) => identity.memberId !== memberId)
if (conflict) {
throw new Error409(
this.options.language,
'errors.alreadyExists',
// @ts-ignore
JSON.stringify({
memberId: filteredExistingIdentities[0].memberId,
memberId: conflict.memberId,
}),
)
}
Expand Down
16 changes: 8 additions & 8 deletions services/libs/data-access-layer/src/members/identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ export async function checkMemberIdentityExistence(
qx: QueryExecutor,
value: string,
platform: string,
type?: MemberIdentityType,
type: MemberIdentityType,
): Promise<IMemberIdentity[]> {
Comment thread
skwowet marked this conversation as resolved.
Outdated
return await qx.select(
return qx.select(
`
SELECT id, "memberId", verified
FROM "memberIdentities"
WHERE "value" = $(value)
AND "platform" = $(platform)
${type ? 'AND "type" = $(type)' : ''}
AND "deletedAt" is null;
SELECT id, "memberId"
FROM "memberIdentities"
WHERE platform = $(platform)
AND type = $(type)
AND lower(value) = lower($(value))
AND "deletedAt" IS NULL;
Comment thread
skwowet marked this conversation as resolved.
Outdated
`,
{
value,
Expand Down
Loading