-
Notifications
You must be signed in to change notification settings - Fork 46
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
base: master
Are you sure you want to change the base?
Conversation
6a27fa6
to
adba519
Compare
Change-type: minor
adba519
to
a8a491f
Compare
TODO: Add getAll and assignToFleet methods |
): Promise<PinePostResult<Team>> { | ||
const orgId = isId(organizationSlugOrId) | ||
? organizationSlugOrId | ||
: (await sdkInstance.models.organization.get(organizationSlugOrId))?.id; |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: { 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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({ |
There was a problem hiding this comment.
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
const team = await get(teamId, { | ||
$select: 'id', | ||
$expand: { belongs_to__organization: { $select: 'id' } }, | ||
}); |
There was a problem hiding this comment.
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 :)
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log 🚓
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; |
There was a problem hiding this comment.
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:
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; |
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}`, | ||
); | ||
} |
There was a problem hiding this comment.
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.
if (isNotFoundResponse(err)) { | ||
throw new Error(`Team not found: ${teamId}`); | ||
} |
There was a problem hiding this comment.
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
if (isNotFoundResponse(err)) { | |
throw new Error(`Team not found: ${teamId}`); | |
} |
Change-type: minor
Resolves: #
HQ:
See:
Depends-on:
Change-type: major|minor|patch
Contributor checklist