import { Response } from "express" ; import { AuthRequest } from "../types/AuthRequest" ; import { getPresentationById , getPresentationsByOrganization , createPresentation , deletePresentationById , } from "../services/presentations" ; import { getExperimentsByIds , getLatestSnapshot , } from "../services/experiments" ; import { getOrgFromReq , userHasAccess } from "../services/organizations" ; import { ExperimentInterface } from "../../types/experiment" ; import { ExperimentSnapshotInterface } from "../../types/experiment-snapshot" ; import { PresentationInterface } from "../../types/presentation" ; export async function getPresentations ( req , res ) { const { org } = getOrgFromReq ( req ) ; const presentations = await getPresentationsByOrganization ( org . id ) ; res . status ( 200 ) . json ( { status : 200 , presentations , } ) ; } export async function getPresentation ( req , res ) { const { id } = req . params ; const pres = await getPresentationById ( id ) ; if ( ! pres ) { res . status ( 403 ) . json ( { status : 404 , message : "Presentation not found" , } ) ; return ; } if ( ! ( await userHasAccess ( req , pres . organization ) ) ) { res . status ( 403 ) . json ( { status : 403 , message : "You do not have access to this presentation" , } ) ; return ; } let expIds = [ ] ; if ( pres . slides ) { expIds = pres . slides . filter ( ( o ) => o . type === "experiment" ) . map ( ( o ) => o . id ) ; } const experiments = await getExperimentsByIds ( expIds ) ; const withSnapshots = [ ] ; const promises = experiments . map ( async ( experiment , i ) => { let phase = experiment . phases . length - 1 ; experiment . phases . forEach ( ( p , j ) => { if ( p . phase === "main" ) phase = j ; } ) ; const snapshot = await getLatestSnapshot ( experiment . id , phase ) ; withSnapshots [ i ] = { experiment , snapshot , } ; } ) ; await Promise . all ( promises ) ; res . status ( 200 ) . json ( { status : 200 , presentation : pres , experiments : withSnapshots , } ) ; } export async function getPresentationPreview ( req , res ) { const { expIds } = req . query as { expIds : string ; } ; if ( ! expIds ) { res . status ( 403 ) . json ( { status : 404 , message : "No experiments passed" , } ) ; return ; } const expIdsArr = expIds . split ( "," ) ; const experiments = await getExperimentsByIds ( expIdsArr ) ; const sortedExps = expIdsArr . map ( ( id ) => { return experiments . filter ( ( o ) => o . id === id ) [ 0 ] ; } ) ; const withSnapshots = [ ] ; const promises = sortedExps . map ( async ( experiment , i ) => { if ( await userHasAccess ( req , experiment . organization ) ) { let phase = experiment . phases . length - 1 ; experiment . phases . forEach ( ( p , j ) => { if ( p . phase === "main" ) phase = j ; } ) ; const snapshot = await getLatestSnapshot ( experiment . id , phase ) ; withSnapshots [ i ] = { experiment , snapshot , } ; } } ) ; await Promise . all ( promises ) ; res . status ( 200 ) . json ( { status : 200 , experiments : withSnapshots , } ) ; } export async function deletePresentation ( req , res ) { const { id } = req . params ; const { org } = getOrgFromReq ( req ) ; const p = await getPresentationById ( id ) ; if ( ! p ) { res . status ( 403 ) . json ( { status : 404 , message : "Presentation not found" , } ) ; return ; } if ( p . organization !== org . id ) { res . status ( 403 ) . json ( { status : 403 , message : "You do not have access to this presentation" , } ) ; return ; } const del = await deletePresentationById ( p . id ) ; res . status ( 200 ) . json ( { status : 200 , result : del , } ) ; } param req param res export async function postPresentation ( req , res ) { const data = req . body ; const { org } = getOrgFromReq ( req ) ; data . organization = org . id ; data . userId = req . userId ; const presentation = await createPresentation ( data ) ; res . status ( 200 ) . json ( { status : 200 , presentation , } ) ; } param req param res export async function updatePresentation ( req , res ) { const { id } = req . params ; const data = req . body ; const { org } = getOrgFromReq ( req ) ; const p = await getPresentationById ( id ) ; if ( ! p ) { res . status ( 403 ) . json ( { status : 404 , message : "Presentation not found" , } ) ; return ; } if ( p . organization !== org . id ) { res . status ( 403 ) . json ( { status : 403 , message : "You do not have access to this presentation" , } ) ; return ; } try { if ( data [ "title" ] !== p [ "title" ] ) p . set ( "title" , data [ "title" ] ) ; if ( data [ "description" ] !== p [ "description" ] ) p . set ( "description" , data [ "description" ] ) ; p . set ( "slides" , data [ "slides" ] ) ; p . set ( "options" , data [ "options" ] ) ; p . set ( "dateUpdated" , new Date ( ) ) ; p . set ( "theme" , data [ "theme" ] ) ; p . set ( "customTheme" , data [ "customTheme" ] ) ; await p . save ( ) ; res . status ( 200 ) . json ( { status : 200 , presentation : p , } ) ; } catch ( e ) { console . log ( "caught error..." ) ; console . error ( e ) ; res . status ( 400 ) . json ( { status : 400 , message : e . message || "An error occurred" , } ) ; } }