This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f27c6b9
commit 818ad11
Showing
5 changed files
with
307 additions
and
0 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
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,95 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
Req, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { groups } from '@prisma/client'; | ||
import { Expose } from 'src/modules/prisma/prisma.interface'; | ||
import { CursorPipe } from 'src/pipes/cursor.pipe'; | ||
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe'; | ||
import { OrderByPipe } from 'src/pipes/order-by.pipe'; | ||
import { WherePipe } from 'src/pipes/where.pipe'; | ||
import { UserRequest } from '../auth/auth.interface'; | ||
import { JwtAuthGuard } from '../auth/jwt-auth.guard'; | ||
import { Scopes } from '../auth/scope.decorator'; | ||
import { ScopesGuard } from '../auth/scope.guard'; | ||
import { CreateGroupDto, ReplaceGroupDto, UpdateGroupDto } from './groups.dto'; | ||
import { GroupsService } from './groups.service'; | ||
|
||
@Controller('groups') | ||
@UseGuards(JwtAuthGuard) | ||
export class GroupController { | ||
constructor(private groupsService: GroupsService) {} | ||
|
||
@Post() | ||
@UseGuards(ScopesGuard) | ||
@Scopes('user:write', 'group:write') | ||
async create( | ||
@Req() req: UserRequest, | ||
@Body() data: CreateGroupDto, | ||
): Promise<Expose<groups>> { | ||
return this.groupsService.createGroup(req.user.id, data); | ||
} | ||
|
||
@Get() | ||
@UseGuards(ScopesGuard) | ||
@Scopes('group:read') | ||
async getAll( | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>, | ||
@Query('where', WherePipe) where?: Record<string, number | string>, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<Expose<groups>[]> { | ||
return this.groupsService.getGroups({ | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where, | ||
}); | ||
} | ||
|
||
@Get(':id') | ||
@UseGuards(ScopesGuard) | ||
@Scopes('group{id}:read') | ||
async get(@Param('id', ParseIntPipe) id: number): Promise<Expose<groups>> { | ||
return this.groupsService.getGroup(Number(id)); | ||
} | ||
|
||
@Patch(':id') | ||
@UseGuards(ScopesGuard) | ||
@Scopes('group{id}:write') | ||
async update( | ||
@Body() data: UpdateGroupDto, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<groups>> { | ||
return this.groupsService.updateGroup(Number(id), data); | ||
} | ||
|
||
@Put(':id') | ||
@UseGuards(ScopesGuard) | ||
@Scopes('group{id}:write') | ||
async replace( | ||
@Body() data: ReplaceGroupDto, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<groups>> { | ||
return this.groupsService.updateGroup(Number(id), data); | ||
} | ||
|
||
@Delete(':id') | ||
@UseGuards(ScopesGuard) | ||
@Scopes('group{id}:delete') | ||
async remove(@Param('id', ParseIntPipe) id: number): Promise<Expose<groups>> { | ||
return this.groupsService.deleteGroup(Number(id)); | ||
} | ||
} |
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,98 @@ | ||
import { | ||
IsArray, | ||
IsBoolean, | ||
IsNotEmpty, | ||
IsObject, | ||
IsOptional, | ||
IsString, | ||
} from 'class-validator'; | ||
|
||
export class CreateGroupDto { | ||
@IsBoolean() | ||
@IsOptional() | ||
autoJoinDomain?: boolean; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
forceTwoFactor?: boolean; | ||
|
||
@IsArray() | ||
@IsOptional() | ||
ipRestrictions?: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
onlyAllowDomain?: boolean; | ||
|
||
@IsString() | ||
@IsOptional() | ||
profilePictureUrl?: string; | ||
|
||
@IsObject() | ||
@IsOptional() | ||
attributes: Record<string, any>; | ||
} | ||
|
||
export class UpdateGroupDto { | ||
@IsBoolean() | ||
@IsOptional() | ||
autoJoinDomain?: boolean; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
forceTwoFactor?: boolean; | ||
|
||
@IsArray() | ||
@IsOptional() | ||
ipRestrictions?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
name?: string; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
onlyAllowDomain?: boolean; | ||
|
||
@IsString() | ||
@IsOptional() | ||
profilePictureUrl?: string; | ||
|
||
@IsObject() | ||
@IsOptional() | ||
attributes: Record<string, any>; | ||
} | ||
|
||
export class ReplaceGroupDto { | ||
@IsBoolean() | ||
@IsNotEmpty() | ||
autoJoinDomain: boolean; | ||
|
||
@IsBoolean() | ||
@IsNotEmpty() | ||
forceTwoFactor: boolean; | ||
|
||
@IsArray() | ||
@IsNotEmpty() | ||
ipRestrictions: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@IsBoolean() | ||
@IsNotEmpty() | ||
onlyAllowDomain: boolean; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
profilePictureUrl: string; | ||
|
||
@IsObject() | ||
@IsNotEmpty() | ||
attributes: Record<string, any>; | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
import { GroupController } from './groups.controller'; | ||
import { GroupsService } from './groups.service'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
controllers: [GroupController], | ||
providers: [GroupsService], | ||
}) | ||
export class GroupsModule {} |
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,101 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { | ||
groups, | ||
groupsCreateInput, | ||
groupsOrderByInput, | ||
groupsUpdateInput, | ||
groupsWhereInput, | ||
groupsWhereUniqueInput, | ||
} from '@prisma/client'; | ||
import { Expose } from 'src/modules/prisma/prisma.interface'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class GroupsService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async createGroup( | ||
userId: number, | ||
data: Omit<Omit<groupsCreateInput, 'group'>, 'user'>, | ||
): Promise<groups> { | ||
return this.prisma.groups.create({ | ||
data: { | ||
...data, | ||
memberships: { | ||
create: { role: 'OWNER', user: { connect: { id: userId } } }, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async getGroups(params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: groupsWhereUniqueInput; | ||
where?: groupsWhereInput; | ||
orderBy?: groupsOrderByInput; | ||
}): Promise<Expose<groups>[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
const groups = await this.prisma.groups.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where, | ||
orderBy, | ||
}); | ||
return groups.map(user => this.prisma.expose<groups>(user)); | ||
} | ||
|
||
async getGroup(id: number): Promise<Expose<groups> | null> { | ||
const group = await this.prisma.groups.findOne({ | ||
where: { id }, | ||
}); | ||
if (!group) | ||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND); | ||
return this.prisma.expose<groups>(group); | ||
} | ||
|
||
async updateGroup( | ||
id: number, | ||
data: groupsUpdateInput, | ||
): Promise<Expose<groups>> { | ||
const testGroup = await this.prisma.groups.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testGroup) | ||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND); | ||
const group = await this.prisma.groups.update({ | ||
where: { id }, | ||
data, | ||
}); | ||
return this.prisma.expose<groups>(group); | ||
} | ||
|
||
async replaceGroup( | ||
id: number, | ||
data: groupsCreateInput, | ||
): Promise<Expose<groups>> { | ||
const testGroup = await this.prisma.groups.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testGroup) | ||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND); | ||
const group = await this.prisma.groups.update({ | ||
where: { id }, | ||
data, | ||
}); | ||
return this.prisma.expose<groups>(group); | ||
} | ||
|
||
async deleteGroup(id: number): Promise<Expose<groups>> { | ||
const testGroup = await this.prisma.groups.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testGroup) | ||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND); | ||
const group = await this.prisma.groups.delete({ | ||
where: { id }, | ||
}); | ||
return this.prisma.expose<groups>(group); | ||
} | ||
} |