Download apps/api/src/services/EntityResolver.ts from safetrack/edtech: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/safetrack/edtech/resolve/main/apps/api/src/services/EntityResolver.ts
- Command line
-
hf download hf://spaces/safetrack/edtech/apps/api/src/services/EntityResolver.ts
-
curl -L -o EntityResolver.ts https://huggingface.co/spaces/safetrack/edtech/resolve/main/apps/api/src/services/EntityResolver.ts
1.7 kB
| import { prisma } from './prisma'; | |
| import { logger } from '../logger'; | |
| export interface ResolvedEntity { | |
| user: any | null; | |
| contact: any | null; | |
| organization: any | null; | |
| activeEnrollment: any | null; | |
| } | |
| export class EntityResolver { | |
| /** | |
| * Resolves User, Contact, Organization and Active Enrollment for a given phone number and organization. | |
| */ | |
| static async resolve(phone: string, organizationId: string): Promise<ResolvedEntity> { | |
| try { | |
| const [user, contact, organization] = await Promise.all([ | |
| prisma.user.findFirst({ | |
| where: { phone, organizationId }, | |
| include: { organization: true } | |
| }), | |
| prisma.contact.findFirst({ | |
| where: { phoneNumber: phone, organizationId } | |
| }), | |
| prisma.organization.findUnique({ | |
| where: { id: organizationId } | |
| }) | |
| ]); | |
| let activeEnrollment = null; | |
| if (user) { | |
| activeEnrollment = await prisma.enrollment.findFirst({ | |
| where: { userId: user.id, status: 'ACTIVE' }, | |
| include: { track: true } | |
| }); | |
| } | |
| return { | |
| user, | |
| contact, | |
| organization: organization || user?.organization || null, | |
| activeEnrollment | |
| }; | |
| } catch (err) { | |
| logger.error({ err, phone, organizationId }, '[EntityResolver] Failed to resolve entity'); | |
| return { user: null, contact: null, organization: null, activeEnrollment: null }; | |
| } | |
| } | |
| } | |