import { Response } from "express" ; import { AuthRequest } from "../types/AuthRequest" ; import { DiscussionParentType } from "../../types/discussion" ; import { addComment , getDiscussionByParent , getLastNDiscussions , } from "../services/discussions" ; import { getFileUploadURL } from "../services/files" ; import { getOrgFromReq } from "../services/organizations" ; export async function postDiscussions ( req , res ) { const { org , userId , email } = getOrgFromReq ( req ) ; const { parentId , parentType } = req . params ; const { comment } = req . body ; try { await addComment ( org . id , parentType , parentId , { id : userId , email : email , name : req ?. name || "" } , comment ) ; res . status ( 200 ) . json ( { status : 200 , } ) ; } catch ( e ) { res . status ( 400 ) . json ( { status : 400 , message : e . message , } ) ; } } export async function deleteComment ( req , res ) { const { org , userId } = getOrgFromReq ( req ) ; const { parentId , parentType , index } = req . params ; const i = parseInt ( index ) ; const discussion = await getDiscussionByParent ( org . id , parentType , parentId ) ; if ( ! discussion ) { return res . status ( 404 ) . json ( { status : 404 , message : "Discussion not found" , } ) ; } const current = discussion . comments [ parseInt ( index ) ] ; if ( current && current ?. userId !== userId ) { return res . status ( 403 ) . json ( { status : 403 , message : "Only the original author can delete a comment" , } ) ; } discussion . comments . splice ( i , 1 ) ; discussion . markModified ( "comments" ) ; try { await discussion . save ( ) ; return res . status ( 200 ) . json ( { status : 200 , } ) ; } catch ( e ) { return res . status ( 400 ) . json ( { status : 400 , message : e . message || "Error deleting comment" , } ) ; } } export async function putComment ( req , res ) { const { org , userId } = getOrgFromReq ( req ) ; const { parentId , parentType , index } = req . params ; const { comment } = req . body ; const i = parseInt ( index ) ; const discussion = await getDiscussionByParent ( org . id , parentType , parentId ) ; if ( ! discussion || ! discussion . comments [ i ] ) { return res . status ( 404 ) . json ( { status : 404 , message : "Discussion not found" , } ) ; } const current = discussion . comments [ i ] ; if ( current . userId !== userId ) { return res . status ( 403 ) . json ( { status : 403 , message : "Only the original author can edit a comment" , } ) ; } current . content = comment ; current . edited = true ; discussion . dateUpdated = new Date ( ) ; discussion . markModified ( "comments" ) ; try { await discussion . save ( ) ; return res . status ( 200 ) . json ( { status : 200 , } ) ; } catch ( e ) { return res . status ( 400 ) . json ( { status : 400 , message : e . message || "Error saving comment" , } ) ; } } export async function getDiscussion ( req , res ) { const { org } = getOrgFromReq ( req ) ; const { parentId , parentType } = req . params ; try { const discussion = await getDiscussionByParent ( org . id , parentType , parentId ) ; res . status ( 200 ) . json ( { status : 200 , discussion , } ) ; } catch ( e ) { res . status ( 400 ) . json ( { status : 400 , message : e . message , } ) ; } } export async function getRecentDiscussions ( req , res ) { const { org } = getOrgFromReq ( req ) ; const { num } = req . params ; let intNum = parseInt ( num ) ; if ( intNum > 100 ) intNum = 100 ; try { const discussions = await getLastNDiscussions ( org . id , intNum + 5 ) ; let recent = [ ] ; discussions . forEach ( ( d ) => { d . comments . forEach ( ( c ) => { recent . push ( { content : c . content , date : c . date , userId : c . userId , userName : c . userName , userEmail : c . userEmail , parentType : d . parentType , parentId : d . parentId , } ) ; } ) ; } ) ; recent = recent . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ; res . status ( 200 ) . json ( { status : 200 , discussions : recent . slice ( 0 , intNum ) , } ) ; } catch ( e ) { res . status ( 400 ) . json ( { status : 400 , message : e . message , } ) ; } } export async function postImageUploadUrl ( req , res ) { const { org } = getOrgFromReq ( req ) ; const { filetype } = req . params ; const now = new Date ( ) ; const { uploadURL , fileURL } = await getFileUploadURL ( filetype , `${ org . id }/${ now . toISOString ( ) . substr ( 0 , 7 ) }/` ) ; res . status ( 200 ) . json ( { status : 200 , uploadURL , fileURL , } ) ; }