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

feat(xo-server/rest-api): ability to update group and its users #8278

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

Conversation

MelissaFrncJrg
Copy link
Contributor

Description

Short explanation of this PR (feel free to re-use commit message)

Checklist

  • Commit
    • Title follows commit conventions
    • Reference the relevant issue (Fixes #007, See xoa-support#42, See https://...)
    • If bug fix, add Introduced by
  • Changelog
    • If visible by XOA users, add changelog entry
    • Update "Packages to release" in CHANGELOG.unreleased.md
  • PR
    • If UI changes, add screenshots
    • If not finished or not tested, open as Draft

Review process

This 2-passes review process aims to:

  • develop skills of junior reviewers
  • limit the workload for senior reviewers
  • limit the number of unnecessary changes by the author
  1. The author creates a PR.
  2. Review process:
    1. The author assigns the junior reviewer.
    2. The junior reviewer conducts their review:
      • Resolves their comments if they are addressed.
      • Adds comments if necessary or approves the PR.
    3. The junior reviewer assigns the senior reviewer.
    4. The senior reviewer conducts their review:
      • If there are no unresolved comments on the PR → merge.
      • Otherwise, we continue with 3.
  3. The author responds to comments and/or makes corrections, and we go back to 2.

Notes:

  1. The author can request a review at any time, even if the PR is still a Draft.
  2. In theory, there should not be more than one reviewer at a time.
  3. The author should not make any changes:
    • When a reviewer is assigned.
    • Between the junior and senior reviews.

@MelissaFrncJrg MelissaFrncJrg marked this pull request as ready for review January 28, 2025 16:21
json(),
wrap(async (req, res) => {
await collections.groups.actions.update(req.body, req)
res.sendStatus(200)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you try to update a group with an ID that doesn't exist, does it return an error code?

@b-Nollet b-Nollet requested a review from MathieuRA January 28, 2025 17:32
Comment on lines +943 to +965
collections.groups.actions = {
update: withParams(
async ({ id, name, userIds }, req) => {
const group = await app.getGroup(id)
if (group.provider !== undefined) {
throw new Error('Cannot edit synchronized group')
}

if (name !== undefined) {
await app.updateGroup(id, { name })
}

if (Array.isArray(userIds)) {
await app.setGroupUsers(id, userIds)
}
},
{
id: { type: 'string' },
name: { type: 'string', optional: true },
userIds: { type: 'array', items: { type: 'string' }, optional: true },
}
),
}
Copy link
Member

Choose a reason for hiding this comment

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

Don't create action like that. Same as: #8276 (comment)

Suggested change
collections.groups.actions = {
update: withParams(
async ({ id, name, userIds }, req) => {
const group = await app.getGroup(id)
if (group.provider !== undefined) {
throw new Error('Cannot edit synchronized group')
}
if (name !== undefined) {
await app.updateGroup(id, { name })
}
if (Array.isArray(userIds)) {
await app.setGroupUsers(id, userIds)
}
},
{
id: { type: 'string' },
name: { type: 'string', optional: true },
userIds: { type: 'array', items: { type: 'string' }, optional: true },
}
),
}

Comment on lines +1541 to +1548
api.patch(
'/groups/update/:id',
json(),
wrap(async (req, res) => {
await collections.groups.actions.update(req.body, req)
res.sendStatus(200)
})
)
Copy link
Member

Choose a reason for hiding this comment

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

IMHO this can be split into 2 routes.
Update group name:

PATCH /:collection(groups)/:id
request body: {name?: string}

Add user to group:

POST /:collection(groups)/:id/users/:id

First route we update a property of the group collection
Second we add a user to the users subresource of the group collection

Comment on lines +946 to +953
const group = await app.getGroup(id)
if (group.provider !== undefined) {
throw new Error('Cannot edit synchronized group')
}

if (name !== undefined) {
await app.updateGroup(id, { name })
}
Copy link
Member

Choose a reason for hiding this comment

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

Avoid adding business logic into the REST API code.
consider the REST API as much as possible as an interface that exposes xo-servers methods via HTTP requests.
IMHO, you can move this logic to xo-mixins/subjects.mjs#updateGroup.

@fbeauchamp What do you think?

@@ -11,6 +11,8 @@

> Users must be able to say: “Nice enhancement, I'm eager to test it”

- [REST API] Ability to update a group's name and its users (PR [#8278](https://github.com/vatesfr/xen-orchestra/pull/8278))
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this message is clear enough. It can be confusing, for example: "you can update the properties of users in the group".
this PR expose the possibility to ADD a user in a group

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about "Allow updating a group's name and manage its users list (add/remove users)" ?

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.

3 participants