Skip to content

Commit

Permalink
edited team controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mertlsarac committed Nov 27, 2022
1 parent d75c787 commit 2fc8278
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
14 changes: 4 additions & 10 deletions app/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
ResponseCode,
TokenUtil,
User,
UserRole,
UserRole
} from '@open-template-hub/common';
import bcrypt from 'bcrypt';
import { Environment } from '../../environment';
Expand Down Expand Up @@ -182,15 +182,9 @@ export class AuthController {
const tokenRepository = new TokenRepository( db );

const userTeams: any[] = await TeamController.getTeams(mongoDbProvider, user.username);

let userTeamIDs: string[] = []

if(userTeams) {
for(const userTeam of userTeams) {
userTeamIDs.push(userTeam._id);
}
}
user.teamIDs = userTeamIDs;
dbUser.teams = userTeams.map( team => {
return team._doc
})

const genereateTokenResponse = await tokenRepository.generateTokens(
dbUser,
Expand Down
20 changes: 12 additions & 8 deletions app/controller/team.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, JoinTeamMailActionParams, MessageQueueChannelType, MongoDbProvider, TokenUtil } from "@open-template-hub/common";
import { TeamRole } from "@open-template-hub/common/lib/enum/team-role.enum";
import { Environment } from "../../environment";
import { TeamRepository } from "../repository/team.repository";

Expand All @@ -11,25 +12,28 @@ export class TeamController {
}

create = async(
context: Context
context: Context,
name: string
) => {
const teamRepository = await new TeamRepository().initialize(
context.mongodb_provider.getConnection()
);

const team = await teamRepository.create(
context.username
context.username,
name
);

const environment = new Environment();
const tokenUtil = new TokenUtil( environment.args() );
const token = tokenUtil.addTeamToToken(context.token, team._id);
const tokens = tokenUtil.addTeamToToken(context.token, team._doc);

return { team, token }
return { tokens }
}

addWriter = async(
context: Context,
teamId: string,
writerUsername: string,
writerEmail: string,
isVerified: boolean
Expand All @@ -49,13 +53,13 @@ export class TeamController {

const orchestrationChannelTag = this.environment.args().mqArgs?.orchestrationServerMessageQueueChannel;

const joinTeamToken = this.tokenUtil.generateJoinTeamToken(writerUsername, team._id, "writers");
const joinTeamToken = this.tokenUtil.generateJoinTeamToken(writerUsername, {id: team._id, role: TeamRole.creator }); // TODO: key uppercase

const joinTeamParams = {
user: writerUsername,
email: writerEmail,
joinTeamToken,
teamId: team._id
team: team
} as JoinTeamMailActionParams

const message = {
Expand Down Expand Up @@ -97,13 +101,13 @@ export class TeamController {

const orchestrationChannelTag = this.environment.args().mqArgs?.orchestrationServerMessageQueueChannel;

const joinTeamToken = this.tokenUtil.generateJoinTeamToken(readerUsername, team._id, "readers")
const joinTeamToken = this.tokenUtil.generateJoinTeamToken(readerUsername, { id: team._id, role: TeamRole.reader } )

const joinTeamParams = {
user: readerUsername,
email: readerEmail,
joinTeamToken,
teamId: team._id
team: team
} as JoinTeamMailActionParams

const message = {
Expand Down
4 changes: 4 additions & 0 deletions app/data/team.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class TeamDataModel {
* This schema should be edited with feature necessities
*/
const schema: mongoose.SchemaDefinition = {
name: {
type: String,
required: true
},
creator: {
type: String,
required: true
Expand Down
7 changes: 5 additions & 2 deletions app/repository/team.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export class TeamRepository {
return this;
};

create = async(creator: string) => {
create = async(creator: string, name: string) => {
try {
return await this.dataModel.create({
creator
creator, name
});
} catch(error) {
console.error( '> createTeam error: ', error );
Expand Down Expand Up @@ -161,6 +161,9 @@ export class TeamRepository {
setQueryKey: true
}}
)
} catch(error) {
console.error( '> verifyTeam error: ', error );
throw error;
}
}

Expand Down
17 changes: 10 additions & 7 deletions app/route/team.route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ResponseCode } from '@open-template-hub/common';
import { ResponseCode, teamAuthorizedBy } from '@open-template-hub/common';
import { TeamRole } from '@open-template-hub/common/lib/enum/team-role.enum';
import { Request, Response, Router } from 'express';
import { TeamController } from '../controller/team.controller';

Expand All @@ -11,7 +12,7 @@ const subRoutes = {

export const router = Router()

router.get(subRoutes.root, async(req: Request, res: Response) => {
router.get(subRoutes.root, teamAuthorizedBy([ TeamRole.creator, TeamRole.reader, TeamRole.writer ]), async(req: Request, res: Response) => {
const context = res.locals.ctx;

const team = await TeamController.getTeams(
Expand All @@ -26,13 +27,14 @@ router.post(subRoutes.root, async(req: Request, res: Response) => {
const teamController = new TeamController();

const teamCreateResponse = await teamController.create(
res.locals.ctx
res.locals.ctx,
req.body.name
);

res.status(ResponseCode.OK).json(teamCreateResponse);
});

router.delete(subRoutes.root, async(req: Request, res: Response) => {
router.delete(subRoutes.root, teamAuthorizedBy([ TeamRole.creator, TeamRole.reader, TeamRole.writer ]), async(req: Request, res: Response) => {
const teamController = new TeamController();

const team = await teamController.deleteTeam(
Expand All @@ -42,11 +44,12 @@ router.delete(subRoutes.root, async(req: Request, res: Response) => {
res.status(ResponseCode.OK);
})

router.post(subRoutes.writer, async(req: Request, res: Response) => {
router.post(subRoutes.writer, teamAuthorizedBy([ TeamRole.creator ]), async(req: Request, res: Response) => {
const teamController = new TeamController();

await teamController.addWriter(
res.locals.ctx,
req.body.teamId,
req.body.writerUsername as string,
req.body.writerEmail as string,
req.body.isVerified as boolean
Expand All @@ -55,10 +58,10 @@ router.post(subRoutes.writer, async(req: Request, res: Response) => {
res.status(ResponseCode.OK);
})

router.get( subRoutes.verify, async ( req: Request, res: Response ) => {
router.get( subRoutes.verify, teamAuthorizedBy([ TeamRole.creator ]), async ( req: Request, res: Response ) => {
const teamController = new TeamController();
const context = res.locals.ctx;
await teamController.verifyWriter(
await teamController.verify(
context.postgresql_provider,
req.query.token as string
);
Expand Down

0 comments on commit 2fc8278

Please sign in to comment.