edtech / apps /api /src /services /EntityResolver.ts
CognxSafeTrack
chore: CRM stabilization sprint completion
4339e77
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 };
}
}
}