208 lines
6.3 KiB
TypeScript
208 lines
6.3 KiB
TypeScript
import { prisma } from '@/services/database';
|
|
import { resolveCollaborator } from '@/services/auth';
|
|
import { getTeamMemberIdsForAdminTeams } from '@/services/teams';
|
|
import { createSessionPermissionChecks } from '@/services/session-permissions';
|
|
import { createShareAndEventHandlers } from '@/services/session-share-events';
|
|
import {
|
|
mergeSessionsByUserId,
|
|
fetchTeamCollaboratorSessions,
|
|
getSessionByIdGeneric,
|
|
} from '@/services/session-queries';
|
|
import type { MotivatorType } from '@prisma/client';
|
|
|
|
const motivatorInclude = {
|
|
user: { select: { id: true, name: true, email: true } },
|
|
shares: { include: { user: { select: { id: true, name: true, email: true } } } },
|
|
_count: { select: { cards: true } },
|
|
};
|
|
|
|
// ============================================
|
|
// Moving Motivators Session CRUD
|
|
// ============================================
|
|
|
|
export async function getMotivatorSessionsByUserId(userId: string) {
|
|
return mergeSessionsByUserId(
|
|
(uid) =>
|
|
prisma.movingMotivatorsSession.findMany({
|
|
where: { userId: uid },
|
|
include: motivatorInclude,
|
|
orderBy: { updatedAt: 'desc' },
|
|
}),
|
|
(uid) =>
|
|
prisma.mMSessionShare.findMany({
|
|
where: { userId: uid },
|
|
include: { session: { include: motivatorInclude } },
|
|
}),
|
|
userId,
|
|
(s) => resolveCollaborator(s.participant).then((r) => ({ resolvedParticipant: r }))
|
|
);
|
|
}
|
|
|
|
/** Sessions owned by team members (where user is team admin) that are NOT shared with the user. */
|
|
export async function getTeamCollaboratorSessionsForAdmin(userId: string) {
|
|
return fetchTeamCollaboratorSessions(
|
|
(teamMemberIds, uid) =>
|
|
prisma.movingMotivatorsSession.findMany({
|
|
where: { userId: { in: teamMemberIds }, shares: { none: { userId: uid } } },
|
|
include: motivatorInclude,
|
|
orderBy: { updatedAt: 'desc' },
|
|
}),
|
|
getTeamMemberIdsForAdminTeams,
|
|
userId,
|
|
(s) => resolveCollaborator(s.participant).then((r) => ({ resolvedParticipant: r }))
|
|
);
|
|
}
|
|
|
|
const motivatorByIdInclude = {
|
|
user: { select: { id: true, name: true, email: true } },
|
|
cards: { orderBy: { orderIndex: 'asc' } as const },
|
|
shares: { include: { user: { select: { id: true, name: true, email: true } } } },
|
|
};
|
|
|
|
export async function getMotivatorSessionById(sessionId: string, userId: string) {
|
|
return getSessionByIdGeneric(
|
|
sessionId,
|
|
userId,
|
|
(sid, uid) =>
|
|
prisma.movingMotivatorsSession.findFirst({
|
|
where: { id: sid, OR: [{ userId: uid }, { shares: { some: { userId: uid } } }] },
|
|
include: motivatorByIdInclude,
|
|
}),
|
|
(sid) =>
|
|
prisma.movingMotivatorsSession.findUnique({ where: { id: sid }, include: motivatorByIdInclude }),
|
|
(s) => resolveCollaborator(s.participant).then((r) => ({ resolvedParticipant: r }))
|
|
);
|
|
}
|
|
|
|
const motivatorPermissions = createSessionPermissionChecks(prisma.movingMotivatorsSession);
|
|
|
|
const motivatorShareEvents = createShareAndEventHandlers<
|
|
'CARD_MOVED' | 'CARD_INFLUENCE_CHANGED' | 'CARDS_REORDERED' | 'SESSION_UPDATED'
|
|
>(
|
|
prisma.movingMotivatorsSession,
|
|
prisma.mMSessionShare,
|
|
prisma.mMSessionEvent,
|
|
motivatorPermissions.canAccess
|
|
);
|
|
|
|
export const canAccessMotivatorSession = motivatorPermissions.canAccess;
|
|
export const canEditMotivatorSession = motivatorPermissions.canEdit;
|
|
export const canDeleteMotivatorSession = motivatorPermissions.canDelete;
|
|
|
|
const DEFAULT_MOTIVATOR_TYPES: MotivatorType[] = [
|
|
'STATUS',
|
|
'POWER',
|
|
'ORDER',
|
|
'ACCEPTANCE',
|
|
'HONOR',
|
|
'MASTERY',
|
|
'SOCIAL',
|
|
'FREEDOM',
|
|
'CURIOSITY',
|
|
'PURPOSE',
|
|
];
|
|
|
|
export async function createMotivatorSession(
|
|
userId: string,
|
|
data: { title: string; participant: string }
|
|
) {
|
|
// Create session with all 10 cards initialized
|
|
return prisma.movingMotivatorsSession.create({
|
|
data: {
|
|
...data,
|
|
userId,
|
|
cards: {
|
|
create: DEFAULT_MOTIVATOR_TYPES.map((type, index) => ({
|
|
type,
|
|
orderIndex: index + 1,
|
|
influence: 0,
|
|
})),
|
|
},
|
|
},
|
|
include: {
|
|
cards: {
|
|
orderBy: { orderIndex: 'asc' },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateMotivatorSession(
|
|
sessionId: string,
|
|
userId: string,
|
|
data: { title?: string; participant?: string }
|
|
) {
|
|
if (!(await canEditMotivatorSession(sessionId, userId))) {
|
|
return { count: 0 };
|
|
}
|
|
return prisma.movingMotivatorsSession.updateMany({
|
|
where: { id: sessionId },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function deleteMotivatorSession(sessionId: string, userId: string) {
|
|
if (!(await canDeleteMotivatorSession(sessionId, userId))) {
|
|
return { count: 0 };
|
|
}
|
|
return prisma.movingMotivatorsSession.deleteMany({
|
|
where: { id: sessionId },
|
|
});
|
|
}
|
|
|
|
// ============================================
|
|
// Motivator Cards CRUD
|
|
// ============================================
|
|
|
|
export async function updateMotivatorCard(
|
|
cardId: string,
|
|
data: { orderIndex?: number; influence?: number }
|
|
) {
|
|
return prisma.motivatorCard.update({
|
|
where: { id: cardId },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function reorderMotivatorCards(sessionId: string, cardIds: string[]) {
|
|
const updates = cardIds.map((id, index) =>
|
|
prisma.motivatorCard.update({
|
|
where: { id },
|
|
data: { orderIndex: index + 1 },
|
|
})
|
|
);
|
|
|
|
return prisma.$transaction(updates);
|
|
}
|
|
|
|
export async function updateCardInfluence(cardId: string, influence: number) {
|
|
// Clamp influence between -3 and +3
|
|
const clampedInfluence = Math.max(-3, Math.min(3, influence));
|
|
return prisma.motivatorCard.update({
|
|
where: { id: cardId },
|
|
data: { influence: clampedInfluence },
|
|
});
|
|
}
|
|
|
|
// ============================================
|
|
// Session Sharing
|
|
// ============================================
|
|
|
|
export const shareMotivatorSession = motivatorShareEvents.share;
|
|
export const removeMotivatorShare = motivatorShareEvents.removeShare;
|
|
export const getMotivatorSessionShares = motivatorShareEvents.getShares;
|
|
|
|
// ============================================
|
|
// Session Events (for real-time sync)
|
|
// ============================================
|
|
|
|
export type MMSessionEventType =
|
|
| 'CARD_MOVED'
|
|
| 'CARD_INFLUENCE_CHANGED'
|
|
| 'CARDS_REORDERED'
|
|
| 'SESSION_UPDATED';
|
|
|
|
export const createMotivatorSessionEvent = motivatorShareEvents.createEvent;
|
|
export const getMotivatorSessionEvents = motivatorShareEvents.getEvents;
|
|
export const getLatestMotivatorEventTimestamp = motivatorShareEvents.getLatestEventTimestamp;
|