Skip to content

Commit 5e87997

Browse files
author
doubleface
committed
feat: Use the right endpoint when creating shared drives
The POST on `/sharings/drives` has more checks than POST on `/sharings` with `drive: true` option
1 parent b1d8d07 commit 5e87997

3 files changed

Lines changed: 141 additions & 3 deletions

File tree

docs/api/cozy-stack-client.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,7 @@ Implements the `DocumentCollection` API along with specific methods for
19821982
* [.renameSharedDrive(sharing, newName)](#SharingCollection+renameSharedDrive) ⇒ <code>object</code>
19831983
* [.fetchSharedDrives()](#SharingCollection+fetchSharedDrives) ⇒ <code>Promise.&lt;{data: Array.&lt;Sharing&gt;}&gt;</code>
19841984
* [.create(params)](#SharingCollection+create)
1985+
* [.createSharedDrive(params)](#SharingCollection+createSharedDrive)
19851986
* [.getDiscoveryLink(sharingId, sharecode, [options])](#SharingCollection+getDiscoveryLink) ⇒ <code>string</code>
19861987
* [.addRecipients(options)](#SharingCollection+addRecipients)
19871988
* [.revokeRecipient(sharing, recipientIndex)](#SharingCollection+revokeRecipient)
@@ -2055,6 +2056,21 @@ Creates a new Sharing. See https://docs.cozy.io/en/cozy-stack/sharing/#post-shar
20552056
| [params.appSlug] | <code>string</code> | Slug of the targeted app |
20562057
| [params.sharedDrive] | <code>boolean</code> | If the sharing is a shared drive |
20572058

2059+
<a name="SharingCollection+createSharedDrive"></a>
2060+
2061+
### sharingCollection.createSharedDrive(params)
2062+
Creates a new shared drive. See https://docs.cozy.io/en/cozy-stack/shared-drives/
2063+
2064+
**Kind**: instance method of [<code>SharingCollection</code>](#SharingCollection)
2065+
2066+
| Param | Type | Description |
2067+
| --- | --- | --- |
2068+
| params | <code>object</code> | Sharing params |
2069+
| params.document | [<code>Sharing</code>](#Sharing) | The document to share. Should have an _id |
2070+
| params.description | <code>string</code> | Description of the sharing |
2071+
| [params.recipients] | [<code>Array.&lt;Recipient&gt;</code>](#Recipient) | Recipients to add to the sharing |
2072+
| [params.readOnlyRecipients] | [<code>Array.&lt;Recipient&gt;</code>](#Recipient) | Recipients to add with read only access |
2073+
20582074
<a name="SharingCollection+getDiscoveryLink"></a>
20592075

20602076
### sharingCollection.getDiscoveryLink(sharingId, sharecode, [options]) ⇒ <code>string</code>

packages/cozy-stack-client/src/SharingCollection.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ class SharingCollection extends DocumentCollection {
150150
appSlug,
151151
sharedDrive
152152
}) {
153+
if (sharedDrive) {
154+
return this.createSharedDrive({
155+
document,
156+
description,
157+
recipients,
158+
readOnlyRecipients
159+
})
160+
}
161+
153162
const attributes = {
154163
description,
155164
preview_path: previewPath,
@@ -160,9 +169,6 @@ class SharingCollection extends DocumentCollection {
160169
if (appSlug) {
161170
optionalAttributes.app_slug = appSlug
162171
}
163-
if (sharedDrive) {
164-
optionalAttributes.drive = sharedDrive
165-
}
166172

167173
const resp = await this.stackClient.fetchJSON('POST', '/sharings/', {
168174
data: {
@@ -183,6 +189,42 @@ class SharingCollection extends DocumentCollection {
183189
return { data: normalizeSharing(resp.data) }
184190
}
185191

192+
/**
193+
* Creates a new shared drive. See https://docs.cozy.io/en/cozy-stack/shared-drives/
194+
*
195+
* @param {object} params Sharing params
196+
* @param {Sharing} params.document The document to share. Should have an _id
197+
* @param {string} params.description Description of the sharing
198+
* @param {Array<Recipient>=} params.recipients Recipients to add to the sharing
199+
* @param {Array<Recipient>=} params.readOnlyRecipients Recipients to add with read only access
200+
*/
201+
async createSharedDrive({
202+
document,
203+
description,
204+
recipients = [],
205+
readOnlyRecipients = []
206+
}) {
207+
const resp = await this.stackClient.fetchJSON('POST', '/sharings/drives', {
208+
data: {
209+
attributes: {
210+
folder_id: document._id,
211+
description
212+
},
213+
relationships: {
214+
...(recipients.length > 0 && {
215+
recipients: { data: recipients.map(toRelationshipItem) }
216+
}),
217+
...(readOnlyRecipients.length > 0 && {
218+
read_only_recipients: {
219+
data: readOnlyRecipients.map(toRelationshipItem)
220+
}
221+
})
222+
}
223+
}
224+
})
225+
return { data: normalizeSharing(resp.data) }
226+
}
227+
186228
/**
187229
* getDiscoveryLink - Returns the URL of the page that can be used to accept a sharing. See https://docs.cozy.io/en/cozy-stack/sharing/#get-sharingssharing-iddiscovery
188230
*

packages/cozy-stack-client/src/SharingCollection.spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,86 @@ describe('SharingCollection', () => {
455455
})
456456
})
457457

458+
describe('createSharedDrive', () => {
459+
beforeEach(() => {
460+
client.fetch.mockReset()
461+
client.fetchJSON.mockResolvedValue({ data: [] })
462+
})
463+
464+
it('should call POST /sharings/drives with folder_id and description', async () => {
465+
await collection.createSharedDrive({
466+
document: FOLDER,
467+
recipients: [RECIPIENT],
468+
description: 'shared drive'
469+
})
470+
471+
expect(client.fetchJSON).toHaveBeenCalledWith(
472+
'POST',
473+
'/sharings/drives',
474+
{
475+
data: {
476+
attributes: {
477+
folder_id: FOLDER._id,
478+
description: 'shared drive'
479+
},
480+
relationships: {
481+
recipients: {
482+
data: [{ id: 'contact_1', type: 'io.cozy.contacts' }]
483+
}
484+
}
485+
}
486+
}
487+
)
488+
})
489+
490+
it('should support read only recipients', async () => {
491+
await collection.createSharedDrive({
492+
document: FOLDER,
493+
readOnlyRecipients: [RECIPIENT],
494+
description: 'test'
495+
})
496+
497+
expect(client.fetchJSON).toHaveBeenCalledWith(
498+
'POST',
499+
'/sharings/drives',
500+
{
501+
data: {
502+
attributes: {
503+
folder_id: FOLDER._id,
504+
description: 'test'
505+
},
506+
relationships: {
507+
read_only_recipients: {
508+
data: [{ id: 'contact_1', type: 'io.cozy.contacts' }]
509+
}
510+
}
511+
}
512+
}
513+
)
514+
})
515+
516+
it('should be called when create is called with sharedDrive: true', async () => {
517+
const spy = jest.spyOn(collection, 'createSharedDrive')
518+
spy.mockResolvedValue({ data: [] })
519+
520+
await collection.create({
521+
document: FOLDER,
522+
recipients: [RECIPIENT],
523+
description: 'test',
524+
sharedDrive: true
525+
})
526+
527+
expect(spy).toHaveBeenCalledWith({
528+
document: FOLDER,
529+
description: 'test',
530+
recipients: [RECIPIENT],
531+
readOnlyRecipients: []
532+
})
533+
534+
spy.mockRestore()
535+
})
536+
})
537+
458538
describe('revokeAllRecipients', () => {
459539
beforeEach(() => {
460540
client.fetch.mockReset()

0 commit comments

Comments
 (0)