|
2 | 2 | import lodash from 'lodash' |
3 | 3 |
|
4 | 4 | import { captureApiChange, memberEditIdentitiesAction } from '@crowd/audit-logs' |
5 | | -import { Error409 } from '@crowd/common' |
| 5 | +import { Error404, Error409 } from '@crowd/common' |
6 | 6 | import { createMemberIdentity, findIdentitiesForMembers, optionsQx } from '@crowd/data-access-layer' |
7 | 7 | import { |
8 | | - checkMemberIdentityExistence, |
9 | 8 | deleteMemberIdentity, |
10 | 9 | fetchMemberIdentities, |
11 | 10 | findMemberIdentityById, |
| 11 | + findMemberIdentityConflict, |
12 | 12 | touchMemberUpdatedAt, |
13 | 13 | updateMemberIdentity, |
14 | 14 | } from '@crowd/data-access-layer/src/members' |
@@ -58,18 +58,19 @@ export default class MemberIdentityService extends LoggerBase { |
58 | 58 | const qx = SequelizeRepository.getQueryExecutor(repoOptions) |
59 | 59 |
|
60 | 60 | // Check if identity already exists |
61 | | - const existingIdentities = await checkMemberIdentityExistence( |
62 | | - qx, |
63 | | - data.value, |
64 | | - data.platform, |
65 | | - ) |
66 | | - if (existingIdentities.length > 0) { |
| 61 | + const conflict = await findMemberIdentityConflict(qx, { |
| 62 | + value: data.value, |
| 63 | + platform: data.platform, |
| 64 | + type: data.type, |
| 65 | + }) |
| 66 | + |
| 67 | + if (conflict) { |
67 | 68 | throw new Error409( |
68 | 69 | this.options.language, |
69 | 70 | 'errors.alreadyExists', |
70 | 71 | // @ts-ignore |
71 | 72 | JSON.stringify({ |
72 | | - memberId: existingIdentities[0].memberId, |
| 73 | + memberId: conflict.memberId, |
73 | 74 | }), |
74 | 75 | ) |
75 | 76 | } |
@@ -130,19 +131,19 @@ export default class MemberIdentityService extends LoggerBase { |
130 | 131 |
|
131 | 132 | // Check if any of the identities already exist |
132 | 133 | for (const identity of data) { |
133 | | - const existingIdentities = await checkMemberIdentityExistence( |
134 | | - qx, |
135 | | - identity.value, |
136 | | - identity.platform, |
137 | | - ) |
| 134 | + const conflict = await findMemberIdentityConflict(qx, { |
| 135 | + value: identity.value, |
| 136 | + platform: identity.platform, |
| 137 | + type: identity.type, |
| 138 | + }) |
138 | 139 |
|
139 | | - if (existingIdentities.length > 0) { |
| 140 | + if (conflict) { |
140 | 141 | throw new Error409( |
141 | 142 | this.options.language, |
142 | 143 | 'errors.alreadyExists', |
143 | 144 | // @ts-ignore |
144 | 145 | JSON.stringify({ |
145 | | - memberId: existingIdentities[0].memberId, |
| 146 | + memberId: conflict.memberId, |
146 | 147 | }), |
147 | 148 | ) |
148 | 149 | } |
@@ -203,20 +204,29 @@ export default class MemberIdentityService extends LoggerBase { |
203 | 204 |
|
204 | 205 | const qx = SequelizeRepository.getQueryExecutor(repoOptions) |
205 | 206 |
|
206 | | - // Check if identity already exists |
207 | | - const existingIdentities = await checkMemberIdentityExistence( |
208 | | - qx, |
209 | | - data.value, |
210 | | - data.platform, |
211 | | - ) |
212 | | - const filteredExistingIdentities = existingIdentities.filter((i) => i.id !== id) |
213 | | - if (filteredExistingIdentities.length > 0) { |
| 207 | + const currentIdentity = memberIdentities.find((identity) => identity.id === id) |
| 208 | + if (!currentIdentity) { |
| 209 | + throw new Error404(this.options.language, 'errors.notFound.message') |
| 210 | + } |
| 211 | + |
| 212 | + const value = data.value ?? currentIdentity.value |
| 213 | + const platform = data.platform ?? currentIdentity.platform |
| 214 | + const type = data.type ?? currentIdentity.type |
| 215 | + |
| 216 | + const conflict = await findMemberIdentityConflict(qx, { |
| 217 | + value, |
| 218 | + platform, |
| 219 | + type, |
| 220 | + excludeMemberId: memberId, |
| 221 | + }) |
| 222 | + |
| 223 | + if (conflict) { |
214 | 224 | throw new Error409( |
215 | 225 | this.options.language, |
216 | 226 | 'errors.alreadyExists', |
217 | 227 | // @ts-ignore |
218 | 228 | JSON.stringify({ |
219 | | - memberId: filteredExistingIdentities[0].memberId, |
| 229 | + memberId: conflict.memberId, |
220 | 230 | }), |
221 | 231 | ) |
222 | 232 | } |
|
0 commit comments