diff --git a/apps/condo/domains/billing/schema/resolvers/accountResolver.js b/apps/condo/domains/billing/schema/resolvers/accountResolver.js index 72da95e750d..e36920d7924 100644 --- a/apps/condo/domains/billing/schema/resolvers/accountResolver.js +++ b/apps/condo/domains/billing/schema/resolvers/accountResolver.js @@ -69,7 +69,7 @@ class AccountResolver extends Resolver { const sameNumberAccount = this.accounts.find(({ number }) => number === accountNumber) if (sameNumberAccount) { const oldBillingProperty = await getById('BillingProperty', sameNumberAccount.property) - const [organizationProperty] = await find('Property', { addressKey: oldBillingProperty.addressKey }) + const [organizationProperty] = await find('Property', { addressKey: oldBillingProperty.addressKey, deletedAt: null, organization: { id: get(this.billingContext, 'organization.id') } }) if (!organizationProperty) { existingAccount = sameNumberAccount } else { diff --git a/apps/condo/domains/billing/schema/resolvers/propertyResolver.js b/apps/condo/domains/billing/schema/resolvers/propertyResolver.js index af289598125..18dc4a46ba5 100644 --- a/apps/condo/domains/billing/schema/resolvers/propertyResolver.js +++ b/apps/condo/domains/billing/schema/resolvers/propertyResolver.js @@ -13,14 +13,14 @@ const { ADDRESS_SERVICE_NORMALIZE_CHUNK_SIZE, } = require('@condo/domains/billing/constants/registerBillingReceiptService') const { Resolver } = require('@condo/domains/billing/schema/resolvers/resolver') -const { isValidFias } = require('@condo/domains/billing/schema/resolvers/utils') +const { isValidFias, normalizePropertyGlobalId } = require('@condo/domains/billing/schema/resolvers/utils') +const { UUID_REGEXP } = require('@condo/domains/common/constants/regexps') const { FLAT_UNIT_TYPE : DEFAULT_UNIT_TYPE, UNIT_TYPES } = require('@condo/domains/property/constants/common') const BILLING_PROPERTY_FIELDS = '{ id importId globalId address addressKey }' const BillingPropertyGQL = generateGqlQueries('BillingProperty', BILLING_PROPERTY_FIELDS) const BillingPropertyApi = generateServerUtils(BillingPropertyGQL) -// FIAS code is not an ordinary uuid class PropertyResolver extends Resolver { @@ -181,6 +181,14 @@ class PropertyResolver extends Resolver { } return receiptIndex } + + addressFieldValue (address, addressKey) { + // In the tests we create addressKey as a md5 hash of address + if (UUID_REGEXP.test(addressKey)) { + return `key:${addressKey}` + } + return address + } async processReceipts (receiptIndex) { const updated = new Set() @@ -196,6 +204,7 @@ class PropertyResolver extends Resolver { receiptIndex[index].problems.push({ problem, params: { addresses } }) } const { importId: importIdInput = '', globalId = '' } = get(receipt, 'addressMeta') || {} + const propertyGlobalId = normalizePropertyGlobalId(globalId) let existingProperty if (importIdInput) { existingProperty = this.properties.find(({ importId }) => importId === importIdInput) @@ -208,8 +217,8 @@ class PropertyResolver extends Resolver { if (!updated.has(existingProperty.id)) { const updateInput = this.buildUpdateInput({ importId: importIdInput, - globalId, - address: resultAddressKey !== existingProperty.addressKey ? address : null, + globalId: propertyGlobalId, + address: resultAddressKey !== existingProperty.addressKey ? this.addressFieldValue(address, resultAddressKey) : null, }, existingProperty) if (!isEmpty(updateInput)) { try { @@ -226,7 +235,9 @@ class PropertyResolver extends Resolver { try { const newProperty = await BillingPropertyApi.create(this.context, this.buildCreateInput({ context: this.billingContext.id, - address, importId: importIdInput, globalId, + address: this.addressFieldValue(address, resultAddressKey), + importId: importIdInput, + globalId: propertyGlobalId, }, ['context'])) this.properties.push(newProperty) receiptIndex[index].property = newProperty.id diff --git a/apps/condo/domains/billing/schema/resolvers/utils.js b/apps/condo/domains/billing/schema/resolvers/utils.js index d929373a641..ab2d5b3398e 100644 --- a/apps/condo/domains/billing/schema/resolvers/utils.js +++ b/apps/condo/domains/billing/schema/resolvers/utils.js @@ -37,6 +37,20 @@ const isPerson = (fullName) => { const isValidFias = (fias = '') => FIAS_REGEXP.test(fias) +/** + * Normalize FIAS code to extract the house part. + * + * @param {string} rawFiasCode - FIAS code containing information about the unit separated with a comma. + * @returns {string|null} Returns the house part of the FIAS code if valid, otherwise returns null. + */ +const normalizePropertyGlobalId = (rawFiasCode) => { + const [fias] = rawFiasCode.split(',') + if (isValidFias(fias)) { + return fias + } + return null +} + const sortPeriodFunction = (periodA, periodB) => (dayjs(periodA, 'YYYY-MM-DD').isAfter(dayjs(periodB, 'YYYY-MM-DD')) ? 1 : -1) module.exports = { @@ -44,4 +58,5 @@ module.exports = { isPerson, isValidFias, sortPeriodFunction, + normalizePropertyGlobalId, } \ No newline at end of file