Skip to content

Commit

Permalink
fix(condo): DOMA-8650 fixed case when address was not resolved correc…
Browse files Browse the repository at this point in the history
…tly (#4548)

* fix(condo): DOMA-8650 fixed case when address was not resolved correctly

* fix(DOMA-8650): fixed tests

* fix(condo): DOMA-8650 review fixes

* fix(condo): DOMA-8650 review fixes
  • Loading branch information
dkoviazin committed Apr 1, 2024
1 parent ef5bb9c commit 6ac1b96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions apps/condo/domains/billing/schema/resolvers/propertyResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions apps/condo/domains/billing/schema/resolvers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,26 @@ 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 = {
clearAccountNumber,
isPerson,
isValidFias,
sortPeriodFunction,
normalizePropertyGlobalId,
}

0 comments on commit 6ac1b96

Please sign in to comment.