Skip to content

Commit

Permalink
feat: all shered components and dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
seba1288 committed Sep 30, 2024
1 parent a8bc1ca commit fc73b3a
Show file tree
Hide file tree
Showing 26 changed files with 412 additions and 108 deletions.
49 changes: 49 additions & 0 deletions src/core/accounts/dtos/account.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Expose, Type } from 'class-transformer';
import { IsDefined } from 'class-validator';
import { GroupDto } from 'src/core/groups/dtos';
import { AccountBasicDto, SystemStatus } from 'src/libs/shared';
import { RoleDto } from 'src/core/role/dtos/role.dto';

export class UserAccountDto extends AccountBasicDto {
@Expose()
firstName?: string;

@Expose()
lastName?: string;

@Expose()
email!: string;

@Expose()
@Type(() => RoleDto)
role!: RoleDto[];

@Expose()
@Type(() => GroupDto)
groups!: GroupDto[];

@Expose()
@IsDefined()
status!: SystemStatus;

get fullName(): string {
const fullName = [this.firstName, this.lastName].filter(Boolean).join(' ');
if (fullName) {
return fullName;
} else {
return this.email;
}
}

get initials(): string {
if (this.firstName && this.lastName) {
return `${this.firstName[0].toUpperCase()}${this.lastName[0].toUpperCase()}`;
} else if (this.firstName) {
return `${this.firstName[0].toUpperCase()}`;
} else if (this.lastName) {
return `${this.lastName[0].toUpperCase()}`;
} else {
return '';
}
}
}
1 change: 1 addition & 0 deletions src/core/accounts/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './account.dto';
29 changes: 29 additions & 0 deletions src/core/groups/dtos/create-group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Optional } from '@nestjs/common';
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString, MaxLength } from 'class-validator';
import { FieldConstraints } from 'src/libs/shared';

export class CreateGroupDto {
@Expose()
@IsString()
@IsNotEmpty()
@MaxLength(FieldConstraints.FIRST_NAME.MAX_LENGTH)
name!: string;

@Expose()
@Optional()
@IsString()
@MaxLength(FieldConstraints.COURSE_NAME.MAX_LENGTH)
courseName?: string;

@Expose()
@Optional()
@IsString()
@MaxLength(FieldConstraints.DESCRIPTION.MAX_LENGTH)
description?: string;

@Expose()
@IsString()
@IsNotEmpty()
avatar!: string;
}
24 changes: 24 additions & 0 deletions src/core/groups/dtos/group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Expose } from 'class-transformer';
import { IsDefined } from 'class-validator';
import { SystemStatus } from 'src/libs/shared';

export class GroupDto {
@Expose()
name!: string;

@Expose()
courseName?: string;

@Expose()
description?: string;

@Expose()
avatar!: string;

@Expose()
code!: string;

@IsDefined()
@Expose()
status!: SystemStatus;
}
3 changes: 3 additions & 0 deletions src/core/groups/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './create-group.dto';
export * from './join-group.dto';
export * from './group.dto';
13 changes: 13 additions & 0 deletions src/core/groups/dtos/join-group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsNotEmpty, IsString, Matches, MaxLength } from 'class-validator';

export class JoinGroupDto {
@IsString()
@IsNotEmpty()
userId!: string;
//TODO: change match & length decorator to use appfield contraints
@IsString()
@IsNotEmpty()
@MaxLength(20)
@Matches(/^[aA-z0-9-]+$/)
code!: string;
}
1 change: 1 addition & 0 deletions src/core/role/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './role.dto';
27 changes: 27 additions & 0 deletions src/core/role/dtos/role.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Expose, Type } from 'class-transformer';
import { IsDefined } from 'class-validator';
import { Action, BaseDto, Subject, SystemStatus } from 'src/libs/shared';

export class PermissionRule {
@Expose()
action!: Action;

@Expose()
subject!: Subject;
}

export class RoleDto extends BaseDto {
@Expose()
name!: string;

@Expose()
code!: string;

@Expose()
@Type(() => PermissionRule)
permissions!: PermissionRule[];

@Expose()
@IsDefined()
status!: SystemStatus;
}
2 changes: 0 additions & 2 deletions src/libs/internal/index.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/libs/shared/consts/fields-contraints.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class FieldConstraints {
};
static readonly USERNAME = {
MAX_LENGTH: 50,
PATTERN: /^[a-zA-Z0-9-_]+$/,
};
static readonly CODE = {
MAX_LENGTH: 50,
Expand All @@ -22,10 +21,12 @@ export class FieldConstraints {
static readonly SUBJECT = {
MAX_LENGTH: 30,
};
static readonly EMAIL = {
MAX_LENGTH: 120,

static readonly COURSE_NAME = {
MAX_LENGTH: 50,
};
static readonly CONTENT = {
PATTERN: /^[a-zA-Z0-9]{6}$/,

static readonly DESCRIPTION = {
MAX_LENGTH: 200,
};
}
14 changes: 14 additions & 0 deletions src/libs/shared/dtos/account-basic.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Expose } from 'class-transformer';
import { BasicDto } from './basic.dto';

export abstract class AccountBasicDto extends BasicDto {
@Expose()
username!: string;

@Expose()
avatarUrl?: string;

abstract fullName: string;

abstract initials: string;
}
31 changes: 31 additions & 0 deletions src/libs/shared/dtos/base.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Exclude, Expose, Transform, Type } from 'class-transformer';
import { BasicDto } from './basic.dto';
import { AccountBasicDto } from './account-basic.dto';
@Exclude()
export abstract class BaseDto extends BasicDto {
@Expose()
@Transform(
({ value, obj }) => {
if (obj.createdBy) {
return value;
} else {
return undefined;
}
},
{ toClassOnly: true },
)
@Type(() => Date)
createdAt?: Date;

@Expose()
@Type(() => Date)
updatedAt!: Date;

@Expose()
@Type(() => AccountBasicDto)
createdBy?: AccountBasicDto;

@Expose()
@Type(() => AccountBasicDto)
updatedBy!: AccountBasicDto;
}
18 changes: 18 additions & 0 deletions src/libs/shared/dtos/basic.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Exclude, Expose, Transform } from 'class-transformer';
@Exclude()
export abstract class BasicDto {
@Expose()
@Transform(
({ value, obj }) => {
if (value && typeof value === 'string') {
return value;
} else if (obj._id) {
return typeof obj._id === 'string' ? obj._id : obj._id.toString();
} else {
return undefined;
}
},
{ toClassOnly: true },
)
id!: string;
}
3 changes: 3 additions & 0 deletions src/libs/shared/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './base.dto';
export * from './basic.dto';
export * from './account-basic.dto';
14 changes: 7 additions & 7 deletions src/libs/shared/enums/event-type.enum.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export enum EventType {
ZAJĘCIA = 'Zajęcia',
SPOTKANIE = 'Spotkanie',
EGZAMIN = 'Egzamin',
KOLOKWIUM = 'Kolokwium',
ODWOLANIE_ZAJEC = 'Odwołanie zajęć',
POPRAWKA = 'Poprawka',
INNE = 'Inne',
CLASSES = 'Zajęcia',
MEETING = 'Spotkanie',
EXAM = 'Egzamin',
QUIZ = 'Kolokwium',
CLASS_CANCELLATION = 'Odwołanie zajęć',
RETAKE = 'Poprawka',
OTHER = 'Inne',
}
1 change: 1 addition & 0 deletions src/libs/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './consts';
export * from './enums';
export * from './dtos';
38 changes: 18 additions & 20 deletions src/models/announcement.model.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { BaseClass } from './base.model';
import { BaseClass } from './base-class.model';
import { AnnouncementTargetType } from 'src/libs/shared';

export type AnnouncementTargetDocument = AnnouncementTarget & Document;
export class AnnouncementTarget {
@Prop({
required: true,
lowercase: true,
trim: true,
enum: AnnouncementTargetType,
})
type!: AnnouncementTargetType;

@Prop({ default: [], required: true, type: [String], select: false })
value!: string[];
}

export const AnnouncementTargetSchema =
SchemaFactory.createForClass(AnnouncementTarget);

export type AnnouncementDocument = Announcement & Document;

@Schema()
Expand All @@ -36,8 +19,23 @@ export class Announcement extends BaseClass {
@Prop({ required: true })
endDate!: Date;

@Prop({ required: true })
target: AnnouncementTarget;
@Prop({ required: true, enum: AnnouncementTargetType })
targetType!: AnnouncementTargetType;

@Prop({ required: false, type: [String], select: false }) // Optional target value (array)
targetValue?: string[];

@Prop({
required: true,
type: String,
ref: 'User',
readonly: true,
select: false,
})
createdBy!: string;

@Prop({ required: true, type: String, ref: 'User' })
updatedBy!: string;

@Prop({ required: false })
expiresAt?: Date;
Expand Down
29 changes: 29 additions & 0 deletions src/models/app-permissions.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { Action, Subject, FieldConstraints } from 'src/libs/shared';

export type AppPermissionsDocument = AppPermissions & Document;

@Schema()
export class AppPermissions {
@Prop({
required: true,
lowercase: true,
trim: true,
enum: Action,
maxlength: FieldConstraints.ACTION.MAX_LENGTH,
})
action!: Action;

@Prop({
required: true,
lowercase: true,
trim: true,
enum: Subject,
maxlength: FieldConstraints.SUBJECT.MAX_LENGTH,
})
subject!: Subject;
}

export const AppPermissionsSchema =
SchemaFactory.createForClass(AppPermissions);
21 changes: 21 additions & 0 deletions src/models/base-class.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Prop } from '@nestjs/mongoose';
import { SystemStatus } from 'src/libs/shared';
import { v4 as uuid } from 'uuid';

export abstract class BaseClass {
@Prop({ default: uuid, readonly: true, select: false })
_id!: string;

get id(): string {
return this._id;
}

@Prop({ required: true, default: SystemStatus.ACTIVE, enum: SystemStatus })
status!: SystemStatus;

@Prop({ required: true, default: Date.now, readonly: true })
createdAt!: Date;

@Prop({ required: true, default: Date.now })
updatedAt!: Date;
}
Loading

0 comments on commit fc73b3a

Please sign in to comment.