All files / src/server/api members.ts

83.33% Statements 15/18
75% Branches 6/8
88.88% Functions 8/9
83.33% Lines 15/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64          10x           18x     18x       7x 7x       3x               3x   3x               2x 2x     2x             1x 1x     1x       2x      
import { z } from "zod";
import type { CollectionMembership } from "@/types";
import type { Database } from "@/lib/db";
import { MembershipRepository } from "@/server/repositories/implementations/MembershipRepository";
 
const CreateMemberSchema = z.object({
  userId: z.string(),
});
 
 
export class MemberController {
  constructor(private membershipRepo: MembershipRepository) {}
 
  static fromDb(db: Database): MemberController {
    return new MemberController(new MembershipRepository(db));
  }
 
  async getByIdForCollection(membershipId: string, collectionId: string): Promise<CollectionMembership | null> {
    const row = await this.membershipRepo.findById(membershipId);
    return row && row.collectionId === collectionId ? row : null;
  }
 
  async listMembers(collectionId: string): Promise<CollectionMembership[]> {
    return this.membershipRepo.findByCollection(collectionId);
  }
 
  async getByUser(userId: string, collectionId: string): Promise<CollectionMembership | null> {
    return this.membershipRepo.findByUser(userId, collectionId);
  }
 
  async addMember(collectionId: string, data: unknown): Promise<CollectionMembership> {
    const parsed = CreateMemberSchema.parse(data);
    // New members always start as curator; flags use DB column defaults.
    return this.membershipRepo.create({
      collectionId,
      userId: parsed.userId,
      role: "curator",
    });
  }
 
  async getMember(membershipId: string): Promise<CollectionMembership> {
    const membership = await this.membershipRepo.findById(membershipId);
    Iif (!membership) {
      throw new Error("Membership not found");
    }
    return membership;
  }
 
  // updateMember removed (purses-2wzz): PATCH /members/[mid] had no UI caller and
  // members have no editable fields under the role model.
 
  async removeMember(membershipId: string): Promise<void> {
    const membership = await this.membershipRepo.findById(membershipId);
    Iif (!membership) {
      throw new Error("Membership not found");
    }
    await this.membershipRepo.delete(membershipId);
  }
 
  async addOwner(collectionId: string, userId: string): Promise<CollectionMembership> {
    return this.membershipRepo.create({ collectionId, userId, role: "owner" });
  }
}