feat: add user operations to backend

This commit is contained in:
Elias Schneider
2022-12-05 10:02:19 +01:00
parent e9526fc039
commit 31b3f6cb2f
11 changed files with 176 additions and 81 deletions

View File

@@ -1,4 +1,4 @@
import { PickType } from "@nestjs/swagger";
import { PickType } from "@nestjs/mapped-types";
import { UserDTO } from "./user.dto";
export class PublicUserDTO extends PickType(UserDTO, ["email"] as const) {}

View File

@@ -0,0 +1,6 @@
import { OmitType, PartialType } from "@nestjs/mapped-types";
import { UserDTO } from "./user.dto";
export class UpdateOwnUserDTO extends PartialType(
OmitType(UserDTO, ["isAdmin"] as const)
) {}

View File

@@ -0,0 +1,4 @@
import { PartialType } from "@nestjs/mapped-types";
import { UserDTO } from "./user.dto";
export class UpdateUserDto extends PartialType(UserDTO) {}

View File

@@ -1,17 +1,25 @@
import { Expose, plainToClass } from "class-transformer";
import { IsEmail, IsNotEmpty, IsOptional, IsString } from "class-validator";
import {
IsEmail,
IsNotEmpty,
IsString,
Length,
Matches,
} from "class-validator";
export class UserDTO {
@Expose()
id: string;
@Expose()
@IsOptional()
@IsString()
@Expose()
@Matches("^[a-zA-Z0-9_.]*$", undefined, {
message: "Username can only contain letters, numbers, dots and underscores",
})
@Length(3, 32)
username: string;
@Expose()
@IsOptional()
@IsEmail()
email: string;
@@ -25,4 +33,10 @@ export class UserDTO {
from(partial: Partial<UserDTO>) {
return plainToClass(UserDTO, partial, { excludeExtraneousValues: true });
}
fromList(partial: Partial<UserDTO>[]) {
return partial.map((part) =>
plainToClass(UserDTO, part, { excludeExtraneousValues: true })
);
}
}