Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for the team model #1520

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Add methods for the team model #1520

wants to merge 2 commits into from

Conversation

JSReds
Copy link
Contributor

@JSReds JSReds commented Jan 12, 2025

Change-type: minor

Resolves: #
HQ:
See:
Depends-on:
Change-type: major|minor|patch


Contributor checklist
  • Includes tests
  • Includes typings
  • Includes updated documentation
  • Includes updated build output

@JSReds JSReds self-assigned this Jan 12, 2025
@JSReds JSReds marked this pull request as draft January 12, 2025 16:05
@JSReds
Copy link
Contributor Author

JSReds commented Jan 12, 2025

TODO:

Add getAll and assignToFleet methods

): Promise<PinePostResult<Team>> {
const orgId = isId(organizationSlugOrId)
? organizationSlugOrId
: (await sdkInstance.models.organization.get(organizationSlugOrId))?.id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine if we always did a GET request to confirm that the org exists and give a more helpful error in case it doesn't.
The isId in most cases is an optimization for methods that the UI uses in batch actions.


const teams = await sdkInstance.models.team.getAllByOrganization(orgId);

if (teams.some((t) => t.name === name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using a $filter in team.getAllByOrganization(orgId); so that we don't have to fetch all teams of the org?
FWIW Given that the API does block this, I would be fine even if we didn't have this check.

$filter: {
belongs_to__organization: isId(organizationSlugOrId)
? organizationSlugOrId
: { handle: organizationSlugOrId },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: { handle: organizationSlugOrId },
: {
$any: {
$alias: 'bto',
$expr: {
bto: { handle: organizationSlugOrId },
},
},
},

options: BalenaSdk.PineOptions<BalenaSdk.Team> = {},
): Promise<BalenaSdk.Team> {
if (teamId == null) {
throw new errors.BalenaInvalidParameterError('id', teamId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new errors.BalenaInvalidParameterError('id', teamId);
throw new errors.BalenaInvalidParameterError('teamId', teamId);

organizationSlugOrId: string | number,
options: BalenaSdk.PineOptions<BalenaSdk.Team> = {},
): Promise<BalenaSdk.Team[]> {
return pine.get({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Mark it as async
  • then add a throw in case organizationSlugOrId is unexpected

Comment on lines +177 to +180
const team = await get(teamId, {
$select: 'id',
$expand: { belongs_to__organization: { $select: 'id' } },
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid the JOIN :)

Suggested change
const team = await get(teamId, {
$select: 'id',
$expand: { belongs_to__organization: { $select: 'id' } },
});
const team = await get(teamId, {
$select: ['id', 'belongs_to__organization'],
});

$select: 'id',
$expand: { belongs_to__organization: { $select: 'id' } },
});
console.log(team);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log 🚓

Comment on lines +186 to +191
const orgId =
Array.isArray(team.belongs_to__organization) &&
team.belongs_to__organization[0] &&
'id' in team.belongs_to__organization[0]
? team.belongs_to__organization[0].id
: undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do

		const teamOptions = {
			$select: ['id', 'belongs_to__organization'],
		} satisfies PineOptions<Team>;
		const team = (await get(teamId, teamOptions)) as PineTypedResult<Team, typeof teamOptions>;

then this will be fully typed and we should be able to do just:

Suggested change
const orgId =
Array.isArray(team.belongs_to__organization) &&
team.belongs_to__organization[0] &&
'id' in team.belongs_to__organization[0]
? team.belongs_to__organization[0].id
: undefined;
const orgId = team.belongs_to__organization.__id;

Comment on lines +196 to +202
const teams = await getAllByOrganization(orgId);

if (teams.some((t) => t.name === newTeamName)) {
throw new Error(
`A team with this name already exists in the organization. Organization: ${orgId}, Name: ${newTeamName}`,
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably also expand the teams w/ a $filter in the original pine query so that we do just 1 request.

Comment on lines +233 to +235
if (isNotFoundResponse(err)) {
throw new Error(`Team not found: ${teamId}`);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik the API will just return 200 when the team (or any OData DELETE to a resource that) doesn't exist, so let's remove the try catch

Suggested change
if (isNotFoundResponse(err)) {
throw new Error(`Team not found: ${teamId}`);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants