import * as express from 'express' ; import { signRequestForUpload } from '../aws-s3' ; import User from '../models/User' ; import Team from '../models/Team' ; import Invitation from '../models/Invitation' ; const router = express . Router ( ) ; router . use ( ( req , res , next ) => { console . log ( 'team member API' , req . path ) ; if ( ! req . user ) { res . status ( 401 ) . json ( { error : 'Unauthorized' } ) ; return ; } next ( ) ; } ) ; router . post ( '/aws/get-signed-request-for-upload-to-s3' , async ( req , res , next ) => { try { const { fileName , fileType , prefix , bucket } = req . body ; const returnData = await signRequestForUpload ( { fileName , fileType , prefix , bucket } ) ; console . log ( bucket ) ; res . json ( returnData ) ; } catch ( err ) { next ( err ) ; } } ) ; router . post ( '/user/update-profile' , async ( req , res , next ) => { try { const { name , avatarUrl } = req . body ; const updatedUser = await User . updateProfile ( { userId : req . user . id , name , avatarUrl } ) ; res . json ( { updatedUser } ) ; } catch ( err ) { next ( err ) ; } } ) ; router . post ( '/user/toggle-theme' , async ( req , res , next ) => { try { const { darkTheme } = req . body ; await User . toggleTheme ( { userId : req . user . id , darkTheme } ) ; res . json ( { done : 1 } ) ; } catch ( err ) { next ( err ) ; } } ) ; async function loadTeamData ( team , userId ) { const initialMembers = await User . getMembersForTeam ( { userId , teamId : team . _id } ) ; let initialInvitations = [ ] ; if ( userId === team . teamLeaderId ) { initialInvitations = await Invitation . getTeamInvitations ( { userId , teamId : team . _id } ) ; } const data = { initialMembers , initialInvitations } ; return data ; } router . post ( '/get-initial-data' , async ( req , res , next ) => { try { const teams = await Team . getAllTeamsForUser ( req . user . id ) ; let selectedTeamSlug = req . body . teamSlug ; if ( ! selectedTeamSlug && teams && teams . length > 0 ) { selectedTeamSlug = teams [ 0 ] . slug ; } for ( const team of teams ) { if ( team . slug === selectedTeamSlug ) { Object . assign ( team , await loadTeamData ( team , req . user . id ) ) ; break ; } } res . json ( { teams } ) ; } catch ( err ) { next ( err ) ; } } ) ; router . get ( '/teams' , async ( req , res , next ) => { try { const teams = await Team . getAllTeamsForUser ( req . user . id ) ; console . log ( teams ) ; res . json ( { teams } ) ; } catch ( err ) { next ( err ) ; } } ) ; router . get ( '/teams/get-members' , async ( req , res , next ) => { try { const users = await User . getMembersForTeam ( { userId : req . user . id , teamId : req . query . teamId as string } ) ; res . json ( { users } ) ; } catch ( err ) { next ( err ) ; } } ) ; export default router ;