Skip to content

Commit

Permalink
application-membership.create: Update description to reflect that t…
Browse files Browse the repository at this point in the history
…he user must already be a member of the organization

Change-type: patch
  • Loading branch information
myarmolinsky committed Dec 16, 2024
1 parent 0e1da29 commit 09e6f9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ balena.models.application.membership.getAllByUser(123).then(function(memberships
<a name="balena.models.application.membership.create"></a>

###### membership.create(options) ⇒ <code>Promise</code>
This method adds a user to an application by their username.
This method adds a user to an application by their username if they are a member of the organization.

**Kind**: static method of [<code>membership</code>](#balena.models.application.membership)
**Summary**: Creates a new membership for an application
Expand Down
18 changes: 16 additions & 2 deletions src/models/application-membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ const getApplicationMembershipModel = function (
* @function
* @memberof balena.models.application.membership
*
* @description This method adds a user to an application by their username.
* @description This method adds a user to an application by their username if they are a member of the organization.
*
* @param {Object} options - membership creation parameters
* @param {String|Number} options.application - application handle (string), or id (number)
Expand All @@ -248,9 +248,23 @@ const getApplicationMembershipModel = function (
PinePostResult<ApplicationMembership>
> {
const [{ id }, roleId] = await Promise.all([
getApplication(application, { $select: 'id' }),
getApplication(application, {
$select: 'id',
$expand: {
organization: {
$select: 'id',
$filter: { organization_membership: { user: { username } } },
},
},
}),
roleName ? getRoleId(roleName) : undefined,
]);
// If the user does not have an organization membership, they cannot be added to an application
if (!id) {
throw new Error(
'It is necessary that each user (Auth) that is member of an application that has an organization, is member of the organization',
);
}
type ApplicationMembershipBase = Omit<ApplicationMembership, 'user'>;
type ApplicationMembershipPostBody = ApplicationMembershipBase & {
username: string;
Expand Down

0 comments on commit 09e6f9c

Please sign in to comment.