-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add cleanRelatedContacts service #1019
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Q } from 'cozy-client' | ||
import { | ||
getHasManyItem, | ||
removeHasManyItem | ||
} from 'cozy-client/dist/associations/HasMany' | ||
import logger from 'cozy-logger' | ||
|
||
import { DOCTYPE_CONTACTS } from '../helpers/doctypes' | ||
|
||
const log = logger.namespace('service/cleanRelatedContactsService') | ||
export const cleanRelatedContactsService = async (client, contactDeletedId) => { | ||
// FIXME: Must be changed when completing the contact relationship functionality. | ||
const allContacts = await client.queryAll( | ||
Q(DOCTYPE_CONTACTS) | ||
.where({ | ||
relationships: { | ||
related: { | ||
data: { | ||
$elemMatch: { | ||
_id: contactDeletedId, | ||
_type: DOCTYPE_CONTACTS | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
.indexFields(['relationships.related.data']) | ||
.limitBy(1000) | ||
) | ||
log('info', `all contacts fetched, ${allContacts.length} contact(s) found`) | ||
|
||
if (allContacts?.length > 0) { | ||
const contactsWithRelatedRelDeleted = allContacts.filter( | ||
contact => | ||
getHasManyItem(contact, 'related', contactDeletedId) !== undefined | ||
) | ||
|
||
log( | ||
'info', | ||
`contact ids with related relationships deleted ${contactsWithRelatedRelDeleted | ||
.map(contact => contact._id) | ||
.join(', ')}` | ||
) | ||
|
||
if (contactsWithRelatedRelDeleted.length > 0) { | ||
log('info', 'updating contacts with related contact deleted') | ||
const allContactsUpdated = contactsWithRelatedRelDeleted.map(contact => | ||
removeHasManyItem(contact, 'related', contactDeletedId) | ||
) | ||
|
||
log('info', 'saving all contacts updated') | ||
await client.saveAll(allContactsUpdated) | ||
} | ||
} | ||
} | ||
Merkur39 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { waitFor } from '@testing-library/react' | ||
|
||
import { cleanRelatedContactsService } from './cleanRelatedContactsService' | ||
|
||
jest.mock('cozy-client', () => ({ | ||
Q: jest.fn(() => ({ | ||
where: jest.fn(() => ({ | ||
indexFields: jest.fn(() => ({ | ||
limitBy: jest.fn() | ||
})) | ||
})) | ||
})) | ||
})) | ||
|
||
const setup = ({ | ||
mockQueryAll = jest.fn(), | ||
mockSaveAll = jest.fn(), | ||
contacts = [] | ||
} = {}) => { | ||
const mockClient = { | ||
queryAll: mockQueryAll.mockResolvedValue(contacts), | ||
saveAll: mockSaveAll | ||
} | ||
|
||
return mockClient | ||
} | ||
|
||
describe('cleanRelatedContactsService', () => { | ||
it('should update contacts with related contact deleted', async () => { | ||
const mockSaveAll = jest.fn() | ||
const contacts = [ | ||
{ | ||
_id: 'contact0', | ||
relationships: { | ||
related: { data: [{ _id: 'contactDeletedId' }] } | ||
} | ||
}, | ||
{ | ||
_id: 'contact1', | ||
relationships: { | ||
related: { data: [] } | ||
} | ||
} | ||
] | ||
const client = setup({ mockSaveAll, contacts }) | ||
|
||
await cleanRelatedContactsService(client, 'contactDeletedId') | ||
|
||
await waitFor(() => { | ||
expect(mockSaveAll).toHaveBeenCalledWith([ | ||
{ | ||
_id: 'contact0', | ||
relationships: { | ||
related: { data: [] } | ||
} | ||
} | ||
]) | ||
}) | ||
}) | ||
|
||
it('should update contacts with related contact deleted and keep other relations', async () => { | ||
const mockSaveAll = jest.fn() | ||
const contacts = [ | ||
{ | ||
_id: 'contact0', | ||
relationships: { | ||
related: { data: [{ _id: 'contactDeletedId' }, { _id: 'OtherId' }] } | ||
} | ||
}, | ||
{ | ||
_id: 'contact1', | ||
relationships: { | ||
related: { data: [] } | ||
} | ||
} | ||
] | ||
const client = setup({ mockSaveAll, contacts }) | ||
|
||
await cleanRelatedContactsService(client, 'contactDeletedId') | ||
|
||
await waitFor(() => { | ||
expect(mockSaveAll).toHaveBeenCalledWith([ | ||
{ | ||
_id: 'contact0', | ||
relationships: { | ||
related: { data: [{ _id: 'OtherId' }] } | ||
} | ||
} | ||
]) | ||
}) | ||
}) | ||
Merkur39 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it('should not update contacts if no related contact deleted', async () => { | ||
const mockSaveAll = jest.fn() | ||
const contacts = [ | ||
{ | ||
_id: 'contact0', | ||
relationships: { | ||
related: { data: [{ _id: 'otherId' }] } | ||
} | ||
}, | ||
{ | ||
_id: 'contact1', | ||
relationships: { | ||
related: { data: [] } | ||
} | ||
} | ||
] | ||
const client = setup({ mockSaveAll, contacts }) | ||
|
||
await cleanRelatedContactsService(client, 'contactDeletedId') | ||
|
||
await waitFor(() => { | ||
expect(mockSaveAll).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import fetch from 'node-fetch' | ||
import { cleanRelatedContactsService } from 'src/helpers/cleanRelatedContactsService' | ||
|
||
import CozyClient from 'cozy-client' | ||
import logger from 'cozy-logger' | ||
|
||
import { schema } from '../../helpers/doctypes' | ||
|
||
const log = logger.namespace('services/cleanRelatedContacts') | ||
|
||
global.fetch = fetch | ||
|
||
const cleanRelatedContacts = async () => { | ||
log('info', 'Start cleanRelatedContacts service') | ||
const client = CozyClient.fromEnv(process.env, { schema }) | ||
const contactDeleted = JSON.parse(process.env.COZY_COUCH_DOC) | ||
zatteo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await cleanRelatedContactsService(client, contactDeleted._id) | ||
|
||
log('info', 'All contacts successfully updated') | ||
} | ||
|
||
cleanRelatedContacts().catch(e => { | ||
log('error', e) | ||
process.exit(1) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
elemMatch
operator is to avoid when possible, because it will force an index full scan.But why not simply use the
include
dsl ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On afterthought, using this query will also make a full-scan, and then query all the the relationships, so not good either...