Skip to content

Commit

Permalink
feat: group settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias committed Jan 19, 2024
1 parent 4ccabe5 commit c744a67
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/Controllers/Http/GroupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,27 @@ export default class GroupController {
console.log(expenses)
return { groups: { id: group.id, name: group.name, expenses: expenses }, users: users }
}

public async update(httpContextContract: HttpContextContract) {
const data = JSON.parse(httpContextContract.request.raw() ?? '{}')
const group = await Group.findOrFail(data.groupId)
group.name = data.name
const groupUsers = await group.related('users').pivotQuery().select('user_id')
console.log(groupUsers)
for (const user of data.users) {
if (!groupUsers.some((guser) => parseInt(guser.user_id) === parseInt(user.id))) {
console.log(user.id)
await group.related('users').attach([user.id])
}
}
for (const user of groupUsers) {
if (!data.users.some((guser) => parseInt(guser.id) === parseInt(user.user_id))) {
console.log(user.user_id)
console.log(data.users.map((user) => user.id))
await group.related('users').detach([user.user_id])
}
}
await group.save()
return this.index(httpContextContract)
}
}

0 comments on commit c744a67

Please sign in to comment.