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.

47 lines
1.5 KiB

  1. import { plainToInstance } from 'class-transformer'
  2. import { MemberDTO } from './memberData'
  3. import { MemberRepository } from '~/domain/models/member/memberRepository'
  4. import { MemberItem } from '~/domain/models/member/member'
  5. export class MemberApplicationService {
  6. constructor(
  7. private readonly repository: MemberRepository
  8. ) {}
  9. public async list(id: string): Promise<MemberDTO[]> {
  10. try {
  11. const items = await this.repository.list(id)
  12. return items.map(item => new MemberDTO(item))
  13. } catch(e: any) {
  14. throw new Error(e.response.data.detail)
  15. }
  16. }
  17. public async create(projectId: string, item: MemberDTO): Promise<void> {
  18. try {
  19. const member = plainToInstance(MemberItem, item)
  20. await this.repository.create(projectId, member)
  21. } catch(e: any) {
  22. throw new Error(e.response.data.detail)
  23. }
  24. }
  25. public async update(projectId: string, item: MemberDTO): Promise<void> {
  26. try {
  27. const member = plainToInstance(MemberItem, item)
  28. await this.repository.update(projectId, member)
  29. } catch(e: any) {
  30. throw new Error(e.response.data.detail)
  31. }
  32. }
  33. public bulkDelete(projectId: string, items: MemberDTO[]): Promise<void> {
  34. const ids = items.map(item => item.id)
  35. return this.repository.bulkDelete(projectId, ids)
  36. }
  37. public async isProjectAdmin(projectId: string, userId: number): Promise<boolean> {
  38. const items = await this.repository.list(projectId)
  39. return items.some((item) => item.user === userId && item.isProjectAdmin)
  40. }
  41. }