import { AuthRequest } from "../types/AuthRequest" ; import { Response } from "express" ; import uniqid from "uniqid" ; import { getDataSourceById } from "../models/DataSourceModel" ; import { createDimension , findDimensionById , findDimensionsByOrganization , updateDimension , deleteDimensionById , } from "../models/DimensionModel" ; import { DimensionInterface } from "../../types/dimension" ; import { getOrgFromReq } from "../services/organizations" ; export async function getAllDimensions ( req , res ) { const { org } = getOrgFromReq ( req ) ; const dimensions = await findDimensionsByOrganization ( org . id ) ; res . status ( 200 ) . json ( { status : 200 , dimensions , } ) ; } export async function postDimensions ( req , res ) { const { org } = getOrgFromReq ( req ) ; const { datasource , name , sql } = req . body ; const datasourceDoc = await getDataSourceById ( datasource , org . id ) ; if ( ! datasourceDoc ) { throw new Error ( "Invalid data source" ) ; } const doc = await createDimension ( { datasource , name , sql , id : uniqid ( "dim_" ) , dateCreated : new Date ( ) , dateUpdated : new Date ( ) , organization : org . id , } ) ; res . status ( 200 ) . json ( { status : 200 , dimension : doc , } ) ; } export async function putDimension ( req , res ) { const { org } = getOrgFromReq ( req ) ; const { id } = req . params ; const dimension = await findDimensionById ( id , org . id ) ; if ( ! dimension ) { throw new Error ( "Could not find dimension" ) ; } const { datasource , name , sql } = req . body ; const datasourceDoc = await getDataSourceById ( datasource , org . id ) ; if ( ! datasourceDoc ) { throw new Error ( "Invalid data source" ) ; } await updateDimension ( id , org . id , { datasource , name , sql , dateUpdated : new Date ( ) , } ) ; res . status ( 200 ) . json ( { status : 200 , dimension , } ) ; } export async function deleteDimension ( req , res ) { const { id } = req . params ; const { org } = getOrgFromReq ( req ) ; const dimension = await findDimensionById ( id , org . id ) ; if ( ! dimension ) { throw new Error ( "Could not find dimension" ) ; } try { await deleteDimensionById ( id , org . id ) ; } catch ( e ) { return res . status ( 400 ) . json ( { status : 400 , message : e . message , } ) ; } res . status ( 200 ) . json ( { status : 200 , } ) ; }