Skip to content

Commit

Permalink
fix: Normalize file relationship response
Browse files Browse the repository at this point in the history
When adding/removing a file relationship, the stack answers with a
{type, id} response. We normalize this response to cope with the {_id,
_type} format used everywhere else.
  • Loading branch information
paultranvan committed Oct 13, 2021
1 parent 54512a8 commit f5f465f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
13 changes: 13 additions & 0 deletions packages/cozy-client/src/CozyClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,16 @@ describe('file creation', () => {
it('should support creating a file with references', async () => {
const { client } = setup()
jest.spyOn(client, 'requestMutation')
client.stackClient.fetchJSON = jest
.fn()
.mockResolvedValueOnce({
data: {
_id: '1337'
}
})
.mockResolvedValueOnce({
data: [{ id: 1, type: 'io.cozy.files' }]
})
await client.create(
'io.cozy.files',
{
Expand All @@ -1766,6 +1776,9 @@ describe('file creation', () => {
icons: [{ _id: 1, _type: 'io.cozy.files' }]
}
)
client.stackClient.fetchJSON = jest.fn().mockResolvedValue({
data: [{ id: '1337', type: 'io.cozy.files' }]
})
const requestMutationCalls = client.requestMutation.mock.calls
const lastCall = requestMutationCalls[requestMutationCalls.length - 1]
expect(lastCall[0]).toEqual(
Expand Down
32 changes: 24 additions & 8 deletions packages/cozy-stack-client/src/FileCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const normalizeFile = file => ({
...file.attributes
})

const normalizeReferences = references => {
return references.map(ref => ({ _type: ref.type, _id: ref.id }))
}

const sanitizeFileName = name => name && name.trim()

/**
Expand Down Expand Up @@ -203,13 +207,16 @@ class FileCollection extends DocumentCollection {
* @param {Array} documents An array of JSON documents having a `_type` and `_id` field.
* @returns {object} The JSON API conformant response.
*/
addReferencedBy(document, documents) {
async addReferencedBy(document, documents) {
const refs = documents.map(d => ({ id: d._id, type: d._type }))
return this.stackClient.fetchJSON(
const resp = await this.stackClient.fetchJSON(
'POST',
uri`/files/${document._id}/relationships/referenced_by`,
{ data: refs }
)
return {
data: normalizeReferences(resp.data)
}
}

/**
Expand All @@ -224,13 +231,16 @@ class FileCollection extends DocumentCollection {
* @param {Array} documents An array of JSON documents having a `_type` and `_id` field.
* @returns {object} The JSON API conformant response.
*/
removeReferencedBy(document, documents) {
async removeReferencedBy(document, documents) {
const refs = documents.map(d => ({ id: d._id, type: d._type }))
return this.stackClient.fetchJSON(
const resp = await this.stackClient.fetchJSON(
'DELETE',
uri`/files/${document._id}/relationships/referenced_by`,
{ data: refs }
)
return {
data: normalizeReferences(resp.data)
}
}

/**
Expand All @@ -245,13 +255,16 @@ class FileCollection extends DocumentCollection {
* @param {Array} documents An array of JSON files having an `_id` field.
* @returns {object} The JSON API conformant response.
*/
addReferencesTo(document, documents) {
async addReferencesTo(document, documents) {
const refs = documents.map(d => ({ id: d._id, type: 'io.cozy.files' }))
return this.stackClient.fetchJSON(
const resp = await this.stackClient.fetchJSON(
'POST',
uri`/data/${document._type}/${document._id}/relationships/references`,
{ data: refs }
)
return {
data: normalizeReferences(resp.data)
}
}

/**
Expand All @@ -266,13 +279,16 @@ class FileCollection extends DocumentCollection {
* @param {Array} documents An array of JSON files having an `_id` field.
* @returns {object} The JSON API conformant response.
*/
removeReferencesTo(document, documents) {
async removeReferencesTo(document, documents) {
const refs = documents.map(d => ({ id: d._id, type: 'io.cozy.files' }))
return this.stackClient.fetchJSON(
const resp = await this.stackClient.fetchJSON(
'DELETE',
uri`/data/${document._type}/${document._id}/relationships/references`,
{ data: refs }
)
return {
data: normalizeReferences(resp.data)
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/cozy-stack-client/src/FileCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ describe('FileCollection', () => {

beforeEach(() => {
spy.mockClear()
spy.mockReturnValue({
data: [{ id: '123', type: 'io.cozy.files' }]
})
})

const file = {
Expand All @@ -366,7 +369,8 @@ describe('FileCollection', () => {
_type: 'io.cozy.photos.albums'
}
]
await collection.addReferencedBy(file, refs)
const res = await collection.addReferencedBy(file, refs)
expect(res.data).toEqual([file])
expect(spy).toMatchSnapshot()
})
it('should remove a reference', async () => {
Expand All @@ -376,7 +380,8 @@ describe('FileCollection', () => {
_type: 'io.cozy.photos.albums'
}
]
await collection.removeReferencedBy(file, refs)
const res = await collection.removeReferencedBy(file, refs)
expect(res.data).toEqual([file])
expect(spy).toMatchSnapshot()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ exports[`FileCollection referencedBy should add a reference 1`] = `
"results": Array [
Object {
"type": "return",
"value": undefined,
"value": Object {
"data": Array [
Object {
"id": "123",
"type": "io.cozy.files",
},
],
},
},
],
}
Expand All @@ -119,7 +126,14 @@ exports[`FileCollection referencedBy should remove a reference 1`] = `
"results": Array [
Object {
"type": "return",
"value": undefined,
"value": Object {
"data": Array [
Object {
"id": "123",
"type": "io.cozy.files",
},
],
},
},
],
}
Expand Down

0 comments on commit f5f465f

Please sign in to comment.