Skip to content

Commit

Permalink
Merge pull request #1505 from balena-io/v21
Browse files Browse the repository at this point in the history
Major v21
  • Loading branch information
myarmolinsky authored Jan 15, 2025
2 parents 304d494 + 01d0d42 commit f863846
Show file tree
Hide file tree
Showing 31 changed files with 622 additions and 1,423 deletions.
250 changes: 24 additions & 226 deletions DOCUMENTATION.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ contain the string `+testsdk` to avoid accidental deletion**

- `TEST_MEMBER_EMAIL`: The email of the account for the membership tests.
- `TEST_MEMBER_PASSWORD`: The password of the account for the membership tests.
- `TEST_MEMBER_USERNAME`: The username of the account for the membership tests.
- `TEST_MEMBER_USERNAME`: The username of the account for the membership tests. This user should be added to the `TEST_USERNAME` user's initial organization.

You also have to provide the following environment variables from an account that doesn't yet exist:

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"author": "Balena Ltd. <[email protected]>",
"license": "Apache-2.0",
"engines": {
"node": ">=18.0"
"node": "^20.12.0 || >= 22.0.0"
},
"devDependencies": {
"@balena/env-parsing": "^1.2.0",
Expand Down Expand Up @@ -119,7 +119,7 @@
"dependencies": {
"@balena/es-version": "^1.0.0",
"@types/json-schema": "^7.0.9",
"@types/node": "^18.19.50",
"@types/node": "^20.17.8",
"abortcontroller-polyfill": "^1.7.1",
"balena-auth": "^6.0.1",
"balena-errors": "^4.9.0",
Expand Down
36 changes: 23 additions & 13 deletions src/models/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,50 @@ const getApiKeysModel = function (
*
* @description This method registers a new api key for the current user with the name given.
*
* @param {String} name - the API key name
* @param {String} [description=null] - the API key description
* @param {Object} createApiKeyParams - an object containing the parameters for the creation of an API key
* @param {String} createApiKeyParams.name - the API key name
* @param {String} createApiKeyParams.expiryDate - the API key expiry date
* @param {String} [createApiKeyParams.description=null] - the API key description
*
* @fulfil {String} - API key
* @returns {Promise}
*
* @example
* balena.models.apiKey.create(apiKeyName).then(function(apiKey) {
* balena.models.apiKey.create({name: apiKeyName, expiryDate: 2030-10-12}).then(function(apiKey) {
* console.log(apiKey);
* });
*
* @example
* balena.models.apiKey.create(apiKeyName, apiKeyDescription).then(function(apiKey) {
* balena.models.apiKey.create({name: apiKeyName, expiryDate: 2030-10-12, description: apiKeyDescription}).then(function(apiKey) {
* console.log(apiKey);
* });
*/
async create(
name: string,
description: string | null = null,
expiryDate: string | null = null,
): Promise<string> {
async create({
name,
expiryDate,
description = null,
}: {
name: string;
expiryDate: string | null;
description?: string | null;
}): Promise<string> {
if (expiryDate === undefined) {
throw new errors.BalenaInvalidParameterError(
'createApiKeyParams.expiryDate',
expiryDate,
);
}
const apiKeyBody: {
name: string;
expiryDate: string | null;
description?: string | null;
expiryDate?: string | null;
} = {
name,
expiryDate,
};
if (typeof description === 'string' && !!description) {
apiKeyBody.description = description;
}
if (typeof expiryDate === 'string' && !!expiryDate) {
apiKeyBody.expiryDate = expiryDate;
}
try {
const { body } = await request.send({
method: 'POST',
Expand Down
31 changes: 28 additions & 3 deletions src/models/application-membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
PineSubmitBody,
InjectedDependenciesParam,
PinePostResult,
PineTypedResult,
} from '..';
import { mergePineOptions } from '../util';

Expand Down Expand Up @@ -225,7 +226,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 @@ -247,10 +248,34 @@ const getApplicationMembershipModel = function (
}: ApplicationMembershipCreationOptions): Promise<
PinePostResult<ApplicationMembership>
> {
const [{ id }, roleId] = await Promise.all([
getApplication(application, { $select: 'id' }),
const appOptions = {
$select: 'id',
$expand: {
organization: {
$select: 'id',
$expand: {
organization_membership: {
$select: 'id',
$filter: {
user: { username },
},
},
},
},
},
} satisfies PineOptions<Application>;
const [{ id, organization }, roleId] = await Promise.all([
getApplication(application, appOptions) as Promise<
PineTypedResult<Application, typeof appOptions>
>,
roleName ? getRoleId(roleName) : undefined,
]);
// If the user does not have an organization membership, they cannot be added to an application
if (organization[0].organization_membership.length === 0) {
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
Loading

0 comments on commit f863846

Please sign in to comment.