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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 15x 12x 1x 21x 22x 5x 5x 6x 6x 13x 8x 1x 1x | import { PrismaClient } from '../../../generated/prisma/client';
import { SpecialistRepository } from '../../../application/specialist/specialist-repository';
import { Specialist } from '../../../domain/specialist/specialist';
interface SpecialistRow {
id: string;
userId: string;
name: string;
specialty: string | null;
phone: string | null;
createdAt: Date;
babies: { babyId: string }[];
shares: { userId: string }[];
}
function toDomain(row: SpecialistRow): Specialist {
return Specialist.restore({
id: row.id,
userId: row.userId,
name: row.name,
specialty: row.specialty,
phone: row.phone,
babyIds: row.babies.map((link) => link.babyId),
sharedWithUserIds: row.shares.map((share) => share.userId),
createdAt: row.createdAt,
});
}
const WITH_LINKS = {
babies: { select: { babyId: true } },
shares: { select: { userId: true } },
} as const;
export class PrismaSpecialistRepository implements SpecialistRepository {
constructor(private readonly prisma: PrismaClient) {}
async findById(id: string): Promise<Specialist | null> {
const row = await this.prisma.specialist.findUnique({ where: { id }, include: WITH_LINKS });
return row ? toDomain(row) : null;
}
async findAllVisibleTo(userId: string): Promise<Specialist[]> {
const rows = await this.prisma.specialist.findMany({
where: {
OR: [
// criou
{ userId },
// compartilhado por nome
{ shares: { some: { userId } } },
// ligado a uma criança que esta pessoa alcança — o acesso é decidido pelo `BabyGuardian`,
// e não por `Baby.userId`, porque é ele que manda desde a partilha de guarda
{ babies: { some: { baby: { guardians: { some: { userId } } } } } },
],
},
include: WITH_LINKS,
orderBy: { name: 'asc' },
});
return rows.map(toDomain);
}
/**
* Grava o profissional e **substitui** os vínculos, em transação.
*
* Substituir e não mesclar: a tela manda a lista inteira de crianças e de compartilhamentos, e
* mesclar tornaria impossível desmarcar a última criança — a operação que "atende nenhuma"
* exige. Tudo numa transação porque um apagar que passa e um inserir que falha deixaria o
* profissional visível para menos gente do que a pessoa pediu, sem aviso.
*/
async save(specialist: Specialist): Promise<void> {
await this.prisma.$transaction([
this.prisma.specialist.upsert({
where: { id: specialist.id },
create: {
id: specialist.id,
userId: specialist.userId,
name: specialist.name,
specialty: specialist.specialty,
phone: specialist.phone,
createdAt: specialist.createdAt,
},
update: {
name: specialist.name,
specialty: specialist.specialty,
phone: specialist.phone,
},
}),
this.prisma.specialistBaby.deleteMany({ where: { specialistId: specialist.id } }),
this.prisma.specialistBaby.createMany({
data: specialist.babyIds.map((babyId) => ({ specialistId: specialist.id, babyId })),
}),
this.prisma.specialistShare.deleteMany({ where: { specialistId: specialist.id } }),
this.prisma.specialistShare.createMany({
data: specialist.sharedWithUserIds.map((userId) => ({ specialistId: specialist.id, userId })),
}),
]);
}
async delete(id: string): Promise<void> {
await this.prisma.specialist.delete({ where: { id } });
}
}
|