You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

2 years ago
2 years ago
2 years ago
  1. import { MemberItem } from '@/domain/models/member/member'
  2. import ApiService from '@/services/api.service'
  3. function toModel(item: { [key: string]: any }): MemberItem {
  4. return new MemberItem(item.id, item.user, item.role, item.username, item.rolename)
  5. }
  6. function toPayload(item: MemberItem): { [key: string]: any } {
  7. return {
  8. id: item.id,
  9. user: item.user,
  10. role: item.role,
  11. username: item.username,
  12. rolename: item.rolename
  13. }
  14. }
  15. export class APIMemberRepository {
  16. constructor(private readonly request = ApiService) {}
  17. async list(projectId: string): Promise<MemberItem[]> {
  18. const url = `/projects/${projectId}/members`
  19. const response = await this.request.get(url)
  20. return response.data.map((item: { [key: string]: any }) => toModel(item))
  21. }
  22. async create(projectId: string, item: MemberItem): Promise<MemberItem> {
  23. const url = `/projects/${projectId}/members`
  24. const payload = toPayload(item)
  25. const response = await this.request.post(url, payload)
  26. return toModel(response.data)
  27. }
  28. async update(projectId: string, item: MemberItem): Promise<MemberItem> {
  29. const url = `/projects/${projectId}/members/${item.id}`
  30. const payload = toPayload(item)
  31. const response = await this.request.patch(url, payload)
  32. return toModel(response.data)
  33. }
  34. async bulkDelete(projectId: string, members: MemberItem[]): Promise<void> {
  35. const url = `/projects/${projectId}/members`
  36. const ids = members.map((member) => member.id)
  37. await this.request.delete(url, { ids })
  38. }
  39. async fetchMyRole(projectId: string): Promise<MemberItem> {
  40. const url = `/projects/${projectId}/my-role`
  41. const response = await this.request.get(url)
  42. return toModel(response.data)
  43. }
  44. }