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 | 23x 23x 23x 9x 14x 16x 14x 14x 2x 12x 4x 4x 4x 4x 7x 3x 4x 4x 2x 10x | import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { SpecialistBabyForbiddenError } from './errors/specialist-baby-forbidden.error';
import { SpecialistShareForbiddenError } from './errors/specialist-share-forbidden.error';
export interface ResolvedLinks {
babyIds: string[];
sharedWithUserIds: string[];
}
/**
* Checks what a caller is allowed to point a professional at, before anything is written.
*
* Two different abuses are being closed here, and neither is exotic:
*
* - **Linking to somebody else's child.** The baby id is a uuid the caller supplies. Without this
* check, knowing one would be enough to attach an entry to a stranger's child — and, through the
* visibility union, to make that entry appear in that family's list.
* - **Pushing an entry into a stranger's list.** A share is a user id, also supplied. Sharing is
* therefore restricted to people who already share a child with the caller: the co-guardians.
* That is exactly the set the interface offers, so the rule never blocks a legitimate choice.
*/
export async function resolveSpecialistLinks(
babyGuardianRepository: BabyGuardianRepository,
userId: string,
babyIds: string[] | undefined,
sharedWithUserIds: string[] | undefined,
): Promise<ResolvedLinks> {
const wantedBabyIds = [...new Set(babyIds ?? [])];
const wantedShares = [...new Set(sharedWithUserIds ?? [])].filter((candidate) => candidate !== userId);
if (wantedBabyIds.length === 0 && wantedShares.length === 0) {
return { babyIds: [], sharedWithUserIds: [] };
}
const ownGuardianships = await babyGuardianRepository.findAllByUser(userId);
const reachableBabyIds = new Set(ownGuardianships.map((guardianship) => guardianship.babyId));
const forbiddenBaby = wantedBabyIds.find((babyId) => !reachableBabyIds.has(babyId));
if (forbiddenBaby) {
throw new SpecialistBabyForbiddenError();
}
if (wantedShares.length > 0) {
const coGuardianIds = new Set<string>();
for (const babyId of reachableBabyIds) {
const guardians = await babyGuardianRepository.findAllByBaby(babyId);
for (const guardian of guardians) {
if (guardian.userId !== userId) {
coGuardianIds.add(guardian.userId);
}
}
}
const forbiddenShare = wantedShares.find((candidate) => !coGuardianIds.has(candidate));
if (forbiddenShare) {
throw new SpecialistShareForbiddenError();
}
}
return { babyIds: wantedBabyIds, sharedWithUserIds: wantedShares };
}
|