Skip to content

Commit

Permalink
feat: Add trim for all properties of the identify
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Jan 3, 2024
1 parent 0362c24 commit 5ea3922
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
13 changes: 12 additions & 1 deletion packages/cozy-clisk/src/launcher/saveIdentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async (contactOrIdentity, accountIdentifier, options) => {
: { contact: contactOrIdentity }
identity.identifier = accountIdentifier

identity.contact = formatIdentityContact(identity.contact)
identity.contact = trimProperties(formatIdentityContact(identity.contact))

// Format contact if needed
if (identity.contact.phone) {
Expand Down Expand Up @@ -172,3 +172,14 @@ export function formatIdentityContact(contactToFormat) {
}
return contact
}

export function trimProperties(obj) {
for (let key in obj) {
if (typeof obj[key] === 'string') {
obj[key] = obj[key].trim()
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
trimProperties(obj[key])
}
}
return obj
}
65 changes: 64 additions & 1 deletion packages/cozy-clisk/src/launcher/saveIdentity.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import saveIdentity, { formatIdentityContact } from './saveIdentity'
import saveIdentity, {
formatIdentityContact,
trimProperties
} from './saveIdentity'

describe('saveIdentity', function () {
const client = {
Expand Down Expand Up @@ -152,3 +155,63 @@ describe('formatIdentityContact', () => {
expect(formattedContact).toEqual(expectedContact)
})
})

describe('trimProperties', () => {
it('should trim all properties', () => {
const contact = {
address: [
{
city: ' CITY',
formattedAddress: '86 CHEMIN DES DAMES 01001 CITY ',
postCode: '01001',
street: {
streetName: ' DES DAMES',
streetNumber: 86,
streetType: 'CHEMIN'
}
}
],
email: ' [email protected] ',
name: {
firstName: 'Chris',
fullName: 'Chris A',
lastName: 'A'
},
phoneNumber: [
{
number: ' 060606060606',
type: 'mobile'
}
]
}

const expectedContact = {
address: [
{
city: 'CITY',
formattedAddress: '86 CHEMIN DES DAMES 01001 CITY',
postCode: '01001',
street: {
streetName: 'DES DAMES',
streetNumber: 86,
streetType: 'CHEMIN'
}
}
],
email: '[email protected]',
name: {
firstName: 'Chris',
fullName: 'Chris A',
lastName: 'A'
},
phoneNumber: [
{
number: '060606060606',
type: 'mobile'
}
]
}

expect(trimProperties(contact)).toEqual(expectedContact)
})
})
16 changes: 14 additions & 2 deletions packages/cozy-konnector-libs/src/libs/saveIdentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const saveIdentity = async (
: { contact: contactOrIdentity }
identity.identifier = accountIdentifier

identity.contact = formatIdentityContact(identity.contact)
identity.contact = trimProperties(formatIdentityContact(identity.contact))
if (options.merge == false) {
// Using cozy client in a none merging strategy here.
const newClient = cozyClient.new
Expand Down Expand Up @@ -193,4 +193,16 @@ function formatIdentityContact(contactToFormat) {
}
return contact
}
module.exports = { saveIdentity, formatIdentityContact }

function trimProperties(obj) {
for (let key in obj) {
if (typeof obj[key] === 'string') {
obj[key] = obj[key].trim()
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
trimProperties(obj[key])
}
}
return obj
}

module.exports = { saveIdentity, formatIdentityContact, trimProperties }
61 changes: 61 additions & 0 deletions packages/cozy-konnector-libs/src/libs/saveIdentity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jest.mock('./cozyclient', () => ({
const updateOrCreate = require('./updateOrCreate')
const saveIdentity = require('./saveIdentity').saveIdentity
const formatIdentityContact = require('./saveIdentity').formatIdentityContact
const trimProperties = require('./saveIdentity').trimProperties
const cozyClient = require('./cozyclient')

describe('saveIdentity', () => {
Expand Down Expand Up @@ -132,3 +133,63 @@ describe('formatIdentityContact', () => {
expect(formattedContact).toEqual(expectedContact)
})
})

describe('trimProperties', () => {
it('should trim all properties', () => {
const contact = {
address: [
{
city: ' CITY',
formattedAddress: '86 CHEMIN DES DAMES 01001 CITY ',
postCode: '01001',
street: {
streetName: ' DES DAMES',
streetNumber: 86,
streetType: 'CHEMIN'
}
}
],
email: ' [email protected] ',
name: {
firstName: 'Chris',
fullName: 'Chris A',
lastName: 'A'
},
phoneNumber: [
{
number: ' 060606060606',
type: 'mobile'
}
]
}

const expectedContact = {
address: [
{
city: 'CITY',
formattedAddress: '86 CHEMIN DES DAMES 01001 CITY',
postCode: '01001',
street: {
streetName: 'DES DAMES',
streetNumber: 86,
streetType: 'CHEMIN'
}
}
],
email: '[email protected]',
name: {
firstName: 'Chris',
fullName: 'Chris A',
lastName: 'A'
},
phoneNumber: [
{
number: '060606060606',
type: 'mobile'
}
]
}

expect(trimProperties(contact)).toEqual(expectedContact)
})
})

0 comments on commit 5ea3922

Please sign in to comment.