Skip to content

Commit

Permalink
feat(address-service): DOMA-8491 lowercase AddressSource.source (#4444)
Browse files Browse the repository at this point in the history
* feat(address-service): DOMA-8491 lowercase AddressSource.source

* feat(address-service): DOMA-8491 fix address source validation
  • Loading branch information
AleX83Xpert authored Mar 6, 2024
1 parent 7cb75a2 commit 19b96c3
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ describe('Address', () => {

const sources = await AddressSource.getAll(adminClient, { address: { id: addressModel.id } })
const helpersHash = hashJSON(helpers)
const fullSource = `${source}|helpers:${helpersHash}`
const fullSource = `${source}|helpers:${helpersHash}`.toLowerCase()

expect(sources).toHaveLength(1)
expect(sources).toEqual(expect.arrayContaining([
Expand Down
23 changes: 15 additions & 8 deletions apps/address-service/domains/address/schema/AddressSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
* Generated by `createschema address.AddressSource 'source:Text;'`
*/

const { get, has } = require('lodash')
const isEmpty = require('lodash/isEmpty')

const { GQLError, GQLErrorCode: { BAD_USER_INPUT } } = require('@open-condo/keystone/errors')
const { historical, versioned, uuided, tracked, softDeleted, dvAndSender } = require('@open-condo/keystone/plugins')
const { GQLListSchema } = require('@open-condo/keystone/schema')
const { GQLListSchema, find } = require('@open-condo/keystone/schema')

const access = require('@address-service/domains/address/access/AddressSource')
const { SOURCE_ALREADY_EXISTS_ERROR } = require('@address-service/domains/address/constants')
const { AddressSource: AddressSourceApi } = require('@address-service/domains/address/utils/serverSchema')


const ERRORS = {
SAME_SOURCE: {
Expand All @@ -33,15 +32,23 @@ const AddressSource = new GQLListSchema('AddressSource', {
isRequired: true,
isUnique: true,
hooks: {
resolveInput: ({ resolvedData, fieldPath }) => {
if (has(resolvedData, fieldPath)) {
return get(resolvedData, fieldPath, '').toLowerCase()
}

return get(resolvedData, fieldPath)
},
validateInput: async ({ resolvedData, fieldPath, context, existingItem, operation }) => {
const value = resolvedData[fieldPath]
const lowerCasedSource = get(resolvedData, fieldPath, '').toLowerCase()
const isCreate = operation === 'create'
const isUpdateAddress = operation === 'update' && resolvedData.source !== existingItem.source
const isUpdateAddress = operation === 'update' && lowerCasedSource !== existingItem.source

if (isCreate || isUpdateAddress) {
const sourceSearch = isCreate ? { source_i: value } : { source: value }
const where = { ...sourceSearch, deletedAt: null }
const sameAddressSourceRows = await AddressSourceApi.getAll(context, where, { first: 1 })
const sameAddressSourceRows = await find('AddressSource', {
source: lowerCasedSource,
deletedAt: null,
})

if (!isEmpty(sameAddressSourceRows)) {
throw new GQLError(ERRORS.SAME_SOURCE, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('AddressSource', () => {
expect(data).toEqual({ 'obj': null })
})
})

test('Source string must be lower-cased', async () => {
const source = `${faker.address.city()}, ${faker.address.street() }, ${faker.datatype.number()}, ${faker.datatype.number()}`
const [obj] = await createTestAddressSource(adminClient, { source })

expect(obj.source).toBe(source.toLowerCase())
})
})

describe('update', () => {
Expand Down Expand Up @@ -162,7 +169,7 @@ describe('AddressSource', () => {
const [updatedObj] = await updateTestAddressSource(adminClient, obj.id, { source: source2 })

expect(updatedObj.source).not.toEqual(source)
expect(updatedObj.source).toEqual(source2)
expect(updatedObj.source).toEqual(source2.toLowerCase())
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { Address, AddressSource } = require('@address-service/domains/address/utils/serverSchema')
const { getByCondition } = require('@open-condo/keystone/schema')

const { Address } = require('@address-service/domains/address/utils/serverSchema')
const { mergeAddressAndHelpers } = require('@address-service/domains/common/utils/services/search/searchServiceUtils')

const { AbstractSearchPlugin } = require('./AbstractSearchPlugin')
Expand All @@ -13,15 +15,16 @@ class SearchBySource extends AbstractSearchPlugin {
// We want to return the same result for the same source. This is the address cache, baby!
const godContext = this.keystoneContext.sudo()

const addressSource = await AddressSource.getOne(godContext, {
source_i: mergeAddressAndHelpers(s, this.helpers),
const addressSource = await getByCondition('AddressSource', {
deletedAt: null,
source: mergeAddressAndHelpers(s, this.helpers).toLowerCase(),
})

if (!addressSource) {
return null
}

const addressFoundBySource = await Address.getOne(godContext, { id: addressSource.address.id, deletedAt: null })
const addressFoundBySource = await Address.getOne(godContext, { id: addressSource.address, deletedAt: null })

return addressFoundBySource ? addressFoundBySource : null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('SearchBySource plugin', () => {

const sources = await AddressSource.getAll(adminClient, { address: { id: addressModel.id } })
const helpersHash = hashJSON(helpers)
const fullSource = `${source}|helpers:${helpersHash}`
const fullSource = `${source}|helpers:${helpersHash}`.toLowerCase()

expect(sources).toHaveLength(1)
expect(sources).toEqual(expect.arrayContaining([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// auto generated by kmigrator
// KMIGRATOR:0004_manual_lowercase_address_source

exports.up = async (knex) => {
await knex.raw(`
BEGIN;
SET statement_timeout = '1500s';
UPDATE "AddressSource" SET source = lower(source);
SET statement_timeout = '10s';
COMMIT;
`)
}

exports.down = async (knex) => {
return
}

0 comments on commit 19b96c3

Please sign in to comment.