Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/api/cozy-stack-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,7 @@ Implements the `DocumentCollection` API along with specific methods for
* [.renameSharedDrive(sharing, newName)](#SharingCollection+renameSharedDrive) ⇒ <code>object</code>
* [.fetchSharedDrives()](#SharingCollection+fetchSharedDrives) ⇒ <code>Promise.&lt;{data: Array.&lt;Sharing&gt;}&gt;</code>
* [.create(params)](#SharingCollection+create)
* [.createSharedDrive(params)](#SharingCollection+createSharedDrive)
* [.getDiscoveryLink(sharingId, sharecode, [options])](#SharingCollection+getDiscoveryLink) ⇒ <code>string</code>
* [.addRecipients(options)](#SharingCollection+addRecipients)
* [.revokeRecipient(sharing, recipientIndex)](#SharingCollection+revokeRecipient)
Expand Down Expand Up @@ -2055,6 +2056,21 @@ Creates a new Sharing. See https://docs.cozy.io/en/cozy-stack/sharing/#post-shar
| [params.appSlug] | <code>string</code> | Slug of the targeted app |
| [params.sharedDrive] | <code>boolean</code> | If the sharing is a shared drive |

<a name="SharingCollection+createSharedDrive"></a>

### sharingCollection.createSharedDrive(params)
Creates a new shared drive. See https://docs.cozy.io/en/cozy-stack/shared-drives/

**Kind**: instance method of [<code>SharingCollection</code>](#SharingCollection)

| Param | Type | Description |
| --- | --- | --- |
| params | <code>object</code> | Sharing params |
| params.document | [<code>Sharing</code>](#Sharing) | The document to share. Should have an _id |
| params.description | <code>string</code> | Description of the sharing |
| [params.recipients] | [<code>Array.&lt;Recipient&gt;</code>](#Recipient) | Recipients to add to the sharing |
| [params.readOnlyRecipients] | [<code>Array.&lt;Recipient&gt;</code>](#Recipient) | Recipients to add with read only access |

<a name="SharingCollection+getDiscoveryLink"></a>

### sharingCollection.getDiscoveryLink(sharingId, sharecode, [options]) ⇒ <code>string</code>
Expand Down
48 changes: 45 additions & 3 deletions packages/cozy-stack-client/src/SharingCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ class SharingCollection extends DocumentCollection {
appSlug,
sharedDrive
}) {
if (sharedDrive) {
return this.createSharedDrive({
document,
description,
recipients,
readOnlyRecipients
})
}

const attributes = {
description,
preview_path: previewPath,
Expand All @@ -160,9 +169,6 @@ class SharingCollection extends DocumentCollection {
if (appSlug) {
optionalAttributes.app_slug = appSlug
}
if (sharedDrive) {
optionalAttributes.drive = sharedDrive
}

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

/**
* Creates a new shared drive. See https://docs.cozy.io/en/cozy-stack/shared-drives/
*
* @param {object} params Sharing params
* @param {Sharing} params.document The document to share. Should have an _id
* @param {string} params.description Description of the sharing
* @param {Array<Recipient>=} params.recipients Recipients to add to the sharing
* @param {Array<Recipient>=} params.readOnlyRecipients Recipients to add with read only access
*/
async createSharedDrive({
document,
description,
recipients = [],
readOnlyRecipients = []
}) {
const resp = await this.stackClient.fetchJSON('POST', '/sharings/drives', {
data: {
attributes: {
folder_id: document._id,
description
},
relationships: {
...(recipients.length > 0 && {
recipients: { data: recipients.map(toRelationshipItem) }
}),
...(readOnlyRecipients.length > 0 && {
read_only_recipients: {
data: readOnlyRecipients.map(toRelationshipItem)
}
})
}
}
})
return { data: normalizeSharing(resp.data) }
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* 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
*
Expand Down
80 changes: 80 additions & 0 deletions packages/cozy-stack-client/src/SharingCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,86 @@ describe('SharingCollection', () => {
})
})

describe('createSharedDrive', () => {
beforeEach(() => {
client.fetch.mockReset()
client.fetchJSON.mockResolvedValue({ data: [] })
})

it('should call POST /sharings/drives with folder_id and description', async () => {
await collection.createSharedDrive({
document: FOLDER,
recipients: [RECIPIENT],
description: 'shared drive'
})

expect(client.fetchJSON).toHaveBeenCalledWith(
'POST',
'/sharings/drives',
{
data: {
attributes: {
folder_id: FOLDER._id,
description: 'shared drive'
},
relationships: {
recipients: {
data: [{ id: 'contact_1', type: 'io.cozy.contacts' }]
}
}
}
}
)
})

it('should support read only recipients', async () => {
await collection.createSharedDrive({
document: FOLDER,
readOnlyRecipients: [RECIPIENT],
description: 'test'
})

expect(client.fetchJSON).toHaveBeenCalledWith(
'POST',
'/sharings/drives',
{
data: {
attributes: {
folder_id: FOLDER._id,
description: 'test'
},
relationships: {
read_only_recipients: {
data: [{ id: 'contact_1', type: 'io.cozy.contacts' }]
}
}
}
}
)
})

it('should be called when create is called with sharedDrive: true', async () => {
const spy = jest.spyOn(collection, 'createSharedDrive')
spy.mockResolvedValue({ data: [] })

await collection.create({
document: FOLDER,
recipients: [RECIPIENT],
description: 'test',
sharedDrive: true
})

expect(spy).toHaveBeenCalledWith({
document: FOLDER,
description: 'test',
recipients: [RECIPIENT],
readOnlyRecipients: []
})

spy.mockRestore()
})
})

describe('revokeAllRecipients', () => {
beforeEach(() => {
client.fetch.mockReset()
Expand Down
Loading