chore: annoucement module with user-group

This commit is contained in:
Matin
2025-01-26 10:57:20 +03:30
parent 6da4b5114c
commit c8c98be6ee
13 changed files with 239 additions and 5 deletions
+67
View File
@@ -0,0 +1,67 @@
import { ApiProperty } from "@nestjs/swagger";
import { ArrayMinSize, IsArray, IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength } from "class-validator";
export class CreateUserGroupDto {
@IsNotEmpty({ message: "نام گروه نمی‌تواند خالی باشد" })
@IsString({ message: "نام گروه باید رشته باشد" })
@MaxLength(100, { message: "نام گروه نمی‌تواند بیش از ۱۰۰ کاراکتر باشد" })
@ApiProperty({
example: "گروه کاربران ویژه",
description: "نام گروه",
})
name: string;
@IsOptional()
@IsString({ message: "توضیحات باید رشته باشد" })
@ApiProperty({
example: "گروه ویژه برای کاربران منتخب",
description: "توضیحات اختیاری گروه",
required: false,
})
description?: string;
@IsNotEmpty()
@IsArray({ message: "لیست کاربران باید آرایه باشد" })
@ArrayMinSize(0, { message: "لیست کاربران نمی‌تواند کوچکتر از ۰ باشد" })
@IsUUID("4", { each: true, message: "شناسه هر کاربر باید UUID معتبر باشد" })
@ApiProperty({
type: [String],
example: ["d3d29a70-5c0a-4aae-a587-8a212c3c8f26"],
description: "آرایه ای از شناسه های کاربران",
required: false,
})
userIds: string[];
}
export class UpdateUserGroupDto {
@IsOptional()
@IsNotEmpty({ message: "نام گروه نمی‌تواند خالی باشد" })
@IsString({ message: "نام گروه باید رشته باشد" })
@MaxLength(100, { message: "نام گروه نمی‌تواند بیش از ۱۰۰ کاراکتر باشد" })
@ApiProperty({
example: "گروه کاربران بروزرسانی شده",
description: "نام جدید گروه",
required: false,
})
name?: string;
@IsOptional()
@IsString({ message: "توضیحات باید رشته باشد" })
@ApiProperty({
example: "توضیحات جدید برای گروه",
description: "توضیحات اختیاری گروه",
required: false,
})
description?: string;
@IsOptional()
@IsArray({ message: "لیست کاربران باید آرایه باشد" })
@IsUUID("4", { each: true, message: "شناسه هر کاربر باید UUID معتبر باشد" })
@ApiProperty({
type: [String],
example: ["d3d29a70-5c0a-4aae-a587-8a212c3c8f26"],
description: "آرایه ای از شناسه های کاربران",
required: false,
})
userIds?: string[];
}
@@ -0,0 +1,22 @@
import { Column, Entity, JoinTable, ManyToMany } from "typeorm";
import { User } from "./user.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { Announcement } from "../../announcements/entities/announcement.entity";
@Entity()
export class UserGroup extends BaseEntity {
@Column({ type: "varchar", length: 100 })
name: string;
@Column({ nullable: true })
description?: string;
@ManyToMany(() => User, (user) => user.groups)
@JoinTable()
users: User[];
@ManyToMany(() => Announcement, (announcement) => announcement.targetGroups)
@JoinTable()
announcements: Announcement[];
}
+5 -1
View File
@@ -1,6 +1,7 @@
import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
import { Column, Entity, ManyToMany, ManyToOne, OneToMany } from "typeorm";
import { Role } from "./role.entity";
import { UserGroup } from "./user-group.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { TicketMessage } from "../../tickets/entities/ticket-message.entity";
import { Ticket } from "../../tickets/entities/ticket.entity";
@@ -39,4 +40,7 @@ export class User extends BaseEntity {
@OneToMany(() => TicketMessage, (ticketMessage) => ticketMessage.author)
ticketMessage: TicketMessage[];
@ManyToMany(() => UserGroup, (group) => group.users)
groups: UserGroup[];
}
@@ -0,0 +1,12 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { UserGroup } from "../entities/user-group.entity";
@Injectable()
export class UserGroupRepository extends Repository<UserGroup> {
constructor(@InjectRepository(UserGroup) userGroupRepository: Repository<UserGroup>) {
super(userGroupRepository.target, userGroupRepository.manager, userGroupRepository.queryRunner);
}
}
+21 -2
View File
@@ -1,17 +1,18 @@
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import slugify from "slugify";
import { Not } from "typeorm";
import { In, Not } from "typeorm";
import { RoleRepository } from "./roles.repository";
import { UserRepository } from "../repositories/users.repository";
// import { roles } from "../../../../database/seeders/role.seeder";
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
import { CheckValidityDTO } from "../DTO/check-validity.dto";
import { UpdateProfileDto } from "../DTO/update-profile.dto";
import { CreateUserGroupDto } from "../DTO/user-group.dto";
import { User } from "../entities/user.entity";
import { RoleEnum } from "../enums/role.enum";
import { ValidityType } from "../enums/validity-type.enum";
import { UserRepository } from "../repositories/users.repository";
@Injectable()
export class UsersService {
@@ -113,4 +114,22 @@ export class UsersService {
// this.logger.log({ createdRoles });
// return createdRoles;
// }
async createUserGroup(createUserGroupDto: CreateUserGroupDto) {
const users = await this.userRepository.find({ where: { id: In(createUserGroupDto.userIds) } });
const userGroup = this.userRepository.create({
...createUserGroupDto,
...users,
});
return await this.userRepository.save(userGroup);
}
///****************************************************** */
// async roleSeeder() {
// const countRole = await this.roleRepository.count();
// if (countRole > 0) return;
// const createdRoles = await this.roleRepository.insert(roles);
// this.logger.log({ createdRoles });
// return createdRoles;
// }
}
+4 -2
View File
@@ -2,15 +2,17 @@ import { Module, OnApplicationBootstrap } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Role } from "./entities/role.entity";
import { UserGroup } from "./entities/user-group.entity";
import { User } from "./entities/user.entity";
import { RoleRepository } from "./providers/roles.repository";
import { UserGroupRepository } from "./providers/user-group.repository";
import { UsersService } from "./providers/users.service";
import { UserRepository } from "./repositories/users.repository";
import { UsersController } from "./users.controller";
@Module({
imports: [TypeOrmModule.forFeature([User, Role])],
providers: [UsersService, UserRepository, RoleRepository],
imports: [TypeOrmModule.forFeature([User, Role, UserGroup])],
providers: [UsersService, UserRepository, RoleRepository, UserGroupRepository],
controllers: [UsersController],
exports: [UsersService, TypeOrmModule],
})