-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 [EXL-68] support group creation page
support group creation page
- Loading branch information
tal-rofe
committed
Sep 18, 2022
1 parent
ab4f426
commit b14ea17
Showing
75 changed files
with
1,734 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
apps/backend/src/modules/user/modules/groups/available-label.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Controller, Get, HttpCode, HttpStatus, Logger, Param } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './groups.routes'; | ||
import { AvailableLabelResponse } from './classes/responses'; | ||
import { AvailableLabelContract } from './queries/contracts/available-label.contract'; | ||
|
||
@ApiTags('Groups') | ||
@Controller(Routes.CONTROLLER) | ||
export class AvailableLabelController { | ||
private readonly logger = new Logger(AvailableLabelController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@ApiOperation({ description: 'Check whether a provided label is availble' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ | ||
description: 'Returns whether the provided label is available', | ||
type: AvailableLabelResponse, | ||
}) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is invalid or missing', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If get availability status of the label' }) | ||
@Get(Routes.AVAILABLE_LABEL) | ||
@HttpCode(HttpStatus.OK) | ||
public async availableLabel( | ||
@CurrentUserId() userId: string, | ||
@Param('label') label: string, | ||
): Promise<AvailableLabelResponse> { | ||
this.logger.log(`Will try to get availability status of label: "${label}" with an Id: "${userId}"`); | ||
|
||
const isAvailable = await this.queryBus.execute<AvailableLabelContract, boolean>( | ||
new AvailableLabelContract(userId, label), | ||
); | ||
|
||
this.logger.log(`Successfully got availability status of label: "${label}" with an Id: "${userId}"`); | ||
|
||
return { | ||
isAvailable, | ||
}; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
apps/backend/src/modules/user/modules/groups/classes/create.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString, MaxLength, MinLength } from 'class-validator'; | ||
|
||
import { IsNullable } from '@/decorators/is-nullable.decorator'; | ||
|
||
export class CreateDto { | ||
@ApiProperty({ type: String, description: 'The label for a group', example: 'Yazif Group' }) | ||
@IsString() | ||
@MinLength(1) | ||
@MaxLength(30) | ||
readonly label!: string; | ||
|
||
@ApiProperty({ | ||
type: String, | ||
description: 'The description for a group', | ||
example: 'Yazif Group is brilliant group', | ||
}) | ||
@IsString() | ||
@IsNullable() | ||
readonly description!: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
apps/backend/src/modules/user/modules/groups/get.contoller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Controller, Get, HttpCode, HttpStatus, Logger, NotFoundException, Param } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './groups.routes'; | ||
import { GetResponse } from './classes/responses'; | ||
import { GetGroupContract } from './queries/contracts/get-group.contract'; | ||
|
||
@ApiTags('Groups') | ||
@Controller(Routes.CONTROLLER) | ||
export class GetController { | ||
private readonly logger = new Logger(GetController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@ApiBearerAuth('access-token') | ||
@ApiOperation({ description: 'Get a group of a user' }) | ||
@ApiOkResponse({ description: "If successfully got a user's group", type: GetResponse }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: "If failed to fetch a user's group" }) | ||
@Get(Routes.GET) | ||
@HttpCode(HttpStatus.OK) | ||
public async getAll( | ||
@CurrentUserId() userId: string, | ||
@Param('group_id') groupId: string, | ||
): Promise<GetResponse> { | ||
this.logger.log(`Will try to fetch all groups belong to use with an Id: "${userId}"`); | ||
|
||
const userGroup = await this.queryBus.execute<GetGroupContract, GetResponse | null>( | ||
new GetGroupContract(userId, groupId), | ||
); | ||
|
||
if (!userGroup) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
this.logger.log(`Successfully got all groups belong to user with an Id: "${userId}"`); | ||
|
||
return userGroup; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/backend/src/modules/user/modules/groups/interfaces/user-group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import type { Group, InlinePolicy } from '@prisma/client'; | ||
import type { Group, InlinePolicy, PolicyLibrary } from '@prisma/client'; | ||
|
||
export type IUserGroupInlinePolicy = Pick<InlinePolicy, 'id' | 'label' | 'library'> & { rulesCount: number }; | ||
|
||
export interface IUserGroupGetAll extends Pick<Group, 'id' | 'label'> { | ||
inlinePolicies: IUserGroupInlinePolicy[]; | ||
librariesNames: PolicyLibrary[]; | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/groups/queries/contracts/available-label.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class AvailableLabelContract { | ||
constructor(public readonly userId: string, public readonly label: string) {} | ||
} |
7 changes: 6 additions & 1 deletion
7
apps/backend/src/modules/user/modules/groups/queries/contracts/create-group.contact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
export class CreateGroupContract { | ||
constructor(public readonly userId: string, public readonly ip: string) {} | ||
constructor( | ||
public readonly userId: string, | ||
public readonly ip: string, | ||
public readonly label: string, | ||
public readonly description: string | null, | ||
) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/groups/queries/contracts/get-group.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class GetGroupContract { | ||
constructor(public readonly userId: string, public readonly groupId: string) {} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/backend/src/modules/user/modules/groups/queries/handlers/available-label.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBGroupService } from '@/modules/database/group.service'; | ||
|
||
import { AvailableLabelContract } from '../contracts/available-label.contract'; | ||
|
||
@QueryHandler(AvailableLabelContract) | ||
export class AvailableLabelHandler implements IQueryHandler<AvailableLabelContract> { | ||
constructor(private readonly dbGroupService: DBGroupService) {} | ||
|
||
execute(contract: AvailableLabelContract) { | ||
return this.dbGroupService.isLabelAvailable(contract.userId, contract.label); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/backend/src/modules/user/modules/groups/queries/handlers/get-group.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBGroupService } from '@/modules/database/group.service'; | ||
|
||
import { GetGroupContract } from '../contracts/get-group.contract'; | ||
|
||
@QueryHandler(GetGroupContract) | ||
export class GetGroupHandler implements IQueryHandler<GetGroupContract> { | ||
constructor(private readonly dbGroupService: DBGroupService) {} | ||
|
||
execute(contract: GetGroupContract) { | ||
return this.dbGroupService.getUserGroup(contract.userId, contract.groupId); | ||
} | ||
} |
9 changes: 8 additions & 1 deletion
9
apps/backend/src/modules/user/modules/groups/queries/handlers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
import { AvailableLabelHandler } from './available-label.handler'; | ||
import { CreateGroupHandler } from './create-group.handler'; | ||
import { GetAllGroupsHandler } from './get-all-groups.handler'; | ||
import { GetGroupHandler } from './get-group.handler'; | ||
|
||
export const QueryHandlers = [CreateGroupHandler, GetAllGroupsHandler]; | ||
export const QueryHandlers = [ | ||
CreateGroupHandler, | ||
GetAllGroupsHandler, | ||
AvailableLabelHandler, | ||
GetGroupHandler, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.