import { fromEditableField, getFieldOrEmpty, mapHistoryEntries, mapVector3History, } from "./dto.utils";
/**
* Convert Investigation database model to Response DTO
* @category Types
* @param {IInvestigation} investigation - The investigation document from the database
* @returns {IInvestigationResponseDto} - The formatted response DTO
*/
export function toInvestigationResponseDto(investigation) {
const authorId = investigation?.metadata?.author?.toString() || null;
const originalRequest = null;
const currentVersionIndex = investigation.currentVersionIndex ?? 0;
// If currentVersionIndex is -1, return empty fields (don't update DB, just send empty response)
const shouldReturnEmptyFields = currentVersionIndex === -1;
const steps = shouldReturnEmptyFields
? []
: investigation?.steps
? investigation.steps.map((step) => {
return {
title: fromEditableField(step.title, null),
descriptionEn: fromEditableField(step.descriptionEn, null),
desiredOutcome: fromEditableField(step.desiredOutcome, null),
alternativeOutcome: fromEditableField(step.alternativeOutcome, null),
skippable: fromEditableField(step.skippable, false),
skippable_after_marking: fromEditableField(step.skippable_after_marking, false),
};
})
: null;
const objects = shouldReturnEmptyFields
? []
: (investigation?.objects ?? []).map((object) => ({
name: fromEditableField(object.name, ""),
objectId: fromEditableField(object.objectId, ""),
photo: object?.photo ? fromEditableField(object.photo, null) : undefined,
position: object?.position
? {
x: object.position.x?.value ?? 0,
y: object.position.y?.value ?? 0,
z: object.position.z?.value ?? 0,
}
: undefined,
rotation: object?.rotation
? {
x: object.rotation.x?.value ?? 0,
y: object.rotation.y?.value ?? 0,
z: object.rotation.z?.value ?? 0,
}
: undefined,
size: object?.size?.value ?? undefined,
isContradicting: object.name?.isContradicting ?? false,
targetFieldName: object.name?.targetFieldName ?? null,
contradictionReason: object.name?.contradictionReason ?? null,
positionHistory: mapVector3History(object?.position),
rotationHistory: mapVector3History(object?.rotation),
sizeHistory: mapHistoryEntries(object?.size?.history),
}));
return {
id: investigation._id.toString(),
status: investigation.status,
isLocked: investigation.isLocked,
lockedBy: investigation.lockedBy?.toString() || null,
lockedAt: investigation.lockedAt,
originalRequest,
title: getFieldOrEmpty(shouldReturnEmptyFields, investigation.title, null),
curriculum: getFieldOrEmpty(shouldReturnEmptyFields, investigation.curriculum, null),
unitNumberAndTitle: getFieldOrEmpty(shouldReturnEmptyFields, investigation.unitNumberAndTitle, null),
grade: getFieldOrEmpty(shouldReturnEmptyFields, investigation.grade, null),
lessonNumberAndTitle: getFieldOrEmpty(shouldReturnEmptyFields, investigation.lessonNumberAndTitle, null),
objectives: getFieldOrEmpty(shouldReturnEmptyFields, investigation.objectives, null),
ngss: getFieldOrEmpty(shouldReturnEmptyFields, investigation.ngss, null),
analyticalFacts: getFieldOrEmpty(shouldReturnEmptyFields, investigation.analyticalFacts, null),
goals: getFieldOrEmpty(shouldReturnEmptyFields, investigation.goals, null),
day: getFieldOrEmpty(shouldReturnEmptyFields, investigation.day, null),
steps,
objects,
metadata: {
dateOfDevelopment: investigation?.metadata?.dateOfDevelopment ?? null,
dateOfDevelopmentDelivery: investigation?.metadata?.dateOfDevelopmentDelivery ?? null,
dateOfPublishing: investigation?.metadata?.dateOfPublishing ?? null,
author: {
id: authorId,
name: null,
email: null,
avatar: null,
},
editors: investigation?.metadata?.editors?.map((id) => id.toString()) || [],
views: investigation?.metadata?.views ?? null,
sourceInvestigation: investigation?.metadata?.sourceInvestigation?.toString() || null,
dateOfCreation: investigation?.metadata?.dateOfCreation ?? null,
dateModified: investigation?.metadata?.dateModified ?? null,
editorsDetailed: [],
},
createdAt: investigation.createdAt,
updatedAt: investigation.updatedAt,
currentVersionIndex: investigation.currentVersionIndex ?? 0,
lastChangeWithAI: investigation.lastChangeWithAI ?? false,
};
}
Source