Skip to content

Commit 5f14de5

Browse files
committed
added createTeamFile
1 parent b06cfdb commit 5f14de5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

app/controller/file.controller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ export class FileController {
3636
throw new Error( 'File upload failed' );
3737
};
3838

39+
createTeamFile = async ( context: Context, teamId: string, file: File ): Promise<any> => {
40+
if ( !this.isValidFile( file ) )
41+
throw new Error(
42+
'File must contain contentType, title, description and data'
43+
);
44+
45+
const serviceClient = await this.getServiceClient(
46+
context.mongodb_provider,
47+
context.serviceKey
48+
);
49+
50+
file = await serviceClient.service.upload( serviceClient.client, file );
51+
52+
if ( file.uploaded ) {
53+
const fileRepository = new FileRepository( context.postgresql_provider );
54+
return fileRepository.saveFile( teamId, file, context.serviceKey );
55+
}
56+
57+
throw new Error( 'File upload failed' );
58+
}
59+
3960
/**
4061
* gets file by id
4162
* @param context context

app/route/file.route.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import {
66
authorizedBy,
77
ResponseCode,
8+
teamAuthorizedBy,
9+
TeamRole,
810
UserRole,
911
} from '@open-template-hub/common';
1012
import { Request, Response } from 'express';
@@ -16,6 +18,7 @@ const subRoutes = {
1618
root: '/',
1719
me: '/me',
1820
public: '/public',
21+
team: '/team'
1922
};
2023

2124
export const router = Router();
@@ -50,3 +53,17 @@ router.get(
5053
res.status(ResponseCode.OK).json({ file });
5154
}
5255
);
56+
57+
router.post(
58+
subRoutes.team,
59+
teamAuthorizedBy( [ TeamRole.CREATOR, TeamRole.WRITER ] ),
60+
async ( req: Request, res: Response ) => {
61+
let id = await fileController.createTeamFile(
62+
res.locals.ctx,
63+
req.body.teamId,
64+
req.body.payload as File
65+
);
66+
67+
res.status(ResponseCode.CREATED).json({ id });
68+
}
69+
);

0 commit comments

Comments
 (0)