Skip to content

Commit

Permalink
fix(condo): DOMA-6859 prevent service consumers duplicates (#3718)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleX83Xpert authored Aug 11, 2023
1 parent f6fcd6e commit 3141812
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Generated by `createservice resident.DiscoverServiceConsumersService --type mutations`
*/
const { get, flatMap } = require('lodash')
const { get, filter, flatMap, omit } = require('lodash')

const { featureToggleManager } = require('@open-condo/featureflags/featureToggleManager')
const { getLogger } = require('@open-condo/keystone/logging')
Expand Down Expand Up @@ -162,28 +162,67 @@ const DiscoverServiceConsumersService = new GQLCustomSchema('DiscoverServiceCons
}
}))
const definedCombinations = combinations.filter(Boolean)

const serviceConsumersData = definedCombinations.map(([resident, account]) => {
const organizationId = get(account, ['context', 'organization', 'id'], null)
const billingContextId = get(account, ['context', 'id'], null)
const [acquiringContextId] = get(organizationsToAcquiringContextsMap, organizationId, [null])

return {
dv,
sender,
resident: resident.id,
accountNumber: account.number,
organization: organizationId,
billingAccount: account.id,
billingIntegrationContext: billingContextId || null,
acquiringIntegrationContext: acquiringContextId || null,
isDiscovered: true,
}
})

// Find already created service consumers to prevent creating of duplicates
const existingServiceConsumers = await ServiceConsumer.getAll(context, {
OR: serviceConsumersData.map((data) => ({
AND: [
{ resident: { id: data.resident } },
{ accountNumber: data.accountNumber },
{ organization: { id: data.organization } },
],
})),
})

const createdServiceConsumers = await Promise.all(
definedCombinations.map(([resident, account]) => {
const organizationId = get(account, ['context', 'organization', 'id'], null)
const billingContextId = get(account, ['context', 'id'], null)
const [acquiringContextId] = get(organizationsToAcquiringContextsMap, organizationId, [null])

return ServiceConsumer.create(context, {
dv,
sender,
resident: { connect: { id: resident.id } },
accountNumber: account.number,
organization: { connect: { id: organizationId } },
billingAccount: { connect: { id: account.id } },
billingIntegrationContext: billingContextId ? { connect: { id: billingContextId } } : null,
acquiringIntegrationContext: acquiringContextId ? { connect: { id: acquiringContextId } } : null,
isDiscovered: true,
serviceConsumersData.map((serviceConsumerData) => {
const [existingServiceConsumer] = filter(existingServiceConsumers, {
resident: { id: serviceConsumerData.resident },
accountNumber: serviceConsumerData.accountNumber,
organization: { id: serviceConsumerData.organization },
})

const data = {
...serviceConsumerData,
resident: { connect: { id: serviceConsumerData.resident } },
organization: { connect: { id: serviceConsumerData.organization } },
billingAccount: { connect: { id: serviceConsumerData.billingAccount } },
billingIntegrationContext: serviceConsumerData.billingIntegrationContext ? { connect: { id: serviceConsumerData.billingIntegrationContext } } : null,
acquiringIntegrationContext: serviceConsumerData.acquiringIntegrationContext ? { connect: { id: serviceConsumerData.acquiringIntegrationContext } } : null,
}

if (existingServiceConsumer) {
return ServiceConsumer.update(context, existingServiceConsumer.id, {
// We don't update organization!
...omit(data, 'organization'),
deletedAt: null,
})
}

return ServiceConsumer.create(context, data)
}),
)

const statistics = {
created: createdServiceConsumers.length,
created: createdServiceConsumers.filter(Boolean).length,
residentsFound: residents.length,
billingAccountsFound: billingAccounts.length,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ describe('DiscoverServiceConsumersService', () => {
unitName: unitName1,
})

// trying to create duplicates
await discoverServiceConsumersByTestClient(admin, { billingAccountsIds: [billingAccount1.id, billingAccount2.id] })

// ...and check for service consumers created immediately
const createdServiceConsumers = await ServiceConsumer.getAll(admin, {
resident: { id: resident1.id },
Expand Down

0 comments on commit 3141812

Please sign in to comment.