Files
dashlibstorage/API.companyuser.md
2026-11-02 00:19:21 +01:00

240 lines
5.8 KiB
Markdown

# companyuser
This document is auto-generated from the JSDoc comments in `src/store` by running `npm run api-doc`.
## `companyuser.add`
*Source: `src/store/companyuser/add.js`*
Adds a company user linking a company to a Google auth account. No admin check: this is the low-level link primitive (company creation, invitations). Use `companyuser.addByEmail` for the admin-guarded flow.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `companyId` | `number` | The id of the company. |
| `googleAuthId` | `number` | The id of the Google auth account. |
| `[role='user']` | `'admin'|'user'` | The role within the company. |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<number>` — The id of the created company user link.
**Throws**
`Error` — 'Invalid role' if the role is not 'admin' or 'user'.
**Example**
```js
const id = await store.companyuser.add(1, 42, 'admin');
// => 7
```
## `companyuser.addByEmail`
*Source: `src/store/companyuser/addByEmail.js`*
Adds an existing Google auth user to the admin's selected company. The added user can then select the company as their own selected company. For emails that are not registered yet use `autoinvite.add`.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `googleAuthId` | `number` | The id of the acting Google auth user (must be admin of the selected company). |
| `input` | `{email: string, role?: 'admin'|'user'}` | Email of the existing user and role (default 'user'). |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<{id: number, googleAuthId: number, email: string, role: 'admin'|'user'}>` — The created company user.
**Throws**
`Error` — 'Not authorized', 'Invalid email', 'Invalid role', 'User not found', 'User already in company'
**Example**
```js
const member = await store.companyuser.addByEmail(42, { email: 'new@example.com', role: 'user' });
```
## `companyuser.delete`
*Source: `src/store/companyuser/delete.js`*
Deletes a company user link by its id.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `id` | `number` | The id of the company user link to delete. |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<void>`
**Example**
```js
await store.companyuser.delete(7);
```
## `companyuser.getByEmail`
*Source: `src/store/companyuser/getByEmail.js`*
Returns the company user ids associated with the given email address.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `email` | `string` | The email address. |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<Array<number>>` — The company user ids.
**Example**
```js
const ids = await store.companyuser.getByEmail('user@example.com');
// => [1, 2, 3]
```
## `companyuser.list`
*Source: `src/store/companyuser/list.js`*
Lists the members of the admin's selected company.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `googleAuthId` | `number` | The id of the Google auth user (must be admin of the selected company). |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<Array<{id: number, googleAuthId: number, email: string, role: 'admin'|'user'}>>` — The company users ordered by email.
**Throws**
`Error` — 'Not authorized' / 'No company selected'
**Example**
```js
const members = await store.companyuser.list(42);
```
## `companyuser.remove`
*Source: `src/store/companyuser/remove.js`*
Removes a member from the admin's selected company. History entries keep existing but lose their author link; if the removed user had this company selected, their selection is cleared.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `googleAuthId` | `number` | The id of the acting Google auth user (must be admin of the selected company). |
| `id` | `number` | The company user id to remove. |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<{id: number, googleAuthId: number}>` — The removed company user.
**Throws**
`Error` — 'Not authorized', 'Company user not found', 'Cannot remove the last admin'
**Example**
```js
await store.companyuser.remove(42, 7);
```
## `companyuser.requireAdmin`
*Source: `src/store/companyuser/requireAdmin.js`*
Resolves the selected company and requires the user to be its admin.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `googleAuthId` | `number` | |
| `[tx]` | `object` | |
**Returns**
`Promise<{companyId: number, companyuserId: number, role: string}>`
**Throws**
`Error` — 'Not authorized' if the user is not an admin of the selected company.
## `companyuser.roles`
*Source: `src/store/companyuser/roles.js`*
Validates a company user role.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `role` | `unknown` | |
**Returns**
`'admin'|'user'`
**Throws**
`Error` — 'Invalid role'
## `companyuser.setRole`
*Source: `src/store/companyuser/setRole.js`*
Changes the role of a member of the admin's selected company.
**Parameters**
| Name | Type | Description |
| --- | --- | --- |
| `googleAuthId` | `number` | The id of the acting Google auth user (must be admin of the selected company). |
| `id` | `number` | The company user id. |
| `role` | `'admin'|'user'` | The new role. |
| `[tx]` | `object` | Optional transaction to run within. |
**Returns**
`Promise<{id: number, googleAuthId: number, role: 'admin'|'user'}>` — The updated company user.
**Throws**
`Error` — 'Not authorized', 'Invalid role', 'Company user not found', 'Cannot demote the last admin'
**Example**
```js
await store.companyuser.setRole(42, 7, 'admin');
```
## `companyuser.withTransaction`
*Source: `src/store/companyuser/withTransaction.js`*
Runs `fn(runner)` inside the given transaction, a new one, or (providers without transaction support) directly.