All files / src/infrastructure/database/repositories prisma-user.repository.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 6/6
100% Lines 8/8

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 65 66 67 68 69 70 71 72 73 74 75                              286x                           67x     454x 454x       72x 72x       224x                                                         2x      
import { PrismaClient } from '../../../generated/prisma/client';
import { UserRepository } from '../../../application/user/user-repository';
import { User } from '../../../domain/user/user';
 
function toDomain(record: {
  id: string;
  email: string;
  passwordHash: string;
  name: string;
  avatarUrl: string | null;
  emailNotificationsEnabled: boolean;
  createdAt: Date;
  sessionVersion: number;
  demoExpiresAt: Date | null;
}): User {
  return User.fromPersistence({
    id: record.id,
    email: record.email,
    passwordHash: record.passwordHash,
    name: record.name,
    avatarUrl: record.avatarUrl,
    emailNotificationsEnabled: record.emailNotificationsEnabled,
    createdAt: record.createdAt,
    sessionVersion: record.sessionVersion,
    demoExpiresAt: record.demoExpiresAt,
  });
}
 
export class PrismaUserRepository implements UserRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findByEmail(email: string): Promise<User | null> {
    const record = await this.prisma.user.findUnique({ where: { email: email.toLowerCase() } });
    return record ? toDomain(record) : null;
  }
 
  async findById(id: string): Promise<User | null> {
    const record = await this.prisma.user.findUnique({ where: { id } });
    return record ? toDomain(record) : null;
  }
 
  async save(user: User): Promise<void> {
    await this.prisma.user.upsert({
      where: { id: user.id },
      create: {
        id: user.id,
        email: user.email,
        passwordHash: user.passwordHash,
        name: user.name,
        avatarUrl: user.avatarUrl,
        emailNotificationsEnabled: user.emailNotificationsEnabled,
        createdAt: user.createdAt,
        sessionVersion: user.sessionVersion,
        demoExpiresAt: user.demoExpiresAt,
      },
      // `demoExpiresAt` is written on create and never here. Every edit rebuilds the entity through
      // `User.create`, and one call site that forgot to carry the expiry over would quietly turn a
      // disposable account into a permanent one. Leaving it out makes the column immutable instead
      // of making every future call site responsible for it.
      update: {
        email: user.email,
        passwordHash: user.passwordHash,
        name: user.name,
        avatarUrl: user.avatarUrl,
        emailNotificationsEnabled: user.emailNotificationsEnabled,
        sessionVersion: user.sessionVersion,
      },
    });
  }
 
  async delete(id: string): Promise<void> {
    await this.prisma.user.delete({ where: { id } });
  }
}