Compare commits
2 Commits
efd468873d
...
eb8d87bd48
| Author | SHA1 | Date | |
|---|---|---|---|
| eb8d87bd48 | |||
| 19e648a299 |
+5
-2
@@ -1,12 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import Joi from 'joi';
|
||||
import { validationSchema } from './config/config.validation';
|
||||
import { UsersModule } from './users/users.module';
|
||||
import { databaseConfig } from './config/typeorm.config';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { OrganModule } from './organ/organ.module';
|
||||
import { ComplaintModule } from './complaint/complaint.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -17,7 +18,9 @@ import { AuthModule } from './auth/auth.module';
|
||||
TypeOrmModule.forRootAsync(databaseConfig()),
|
||||
HttpModule.register({global: true, timeout: 10000, headers: {"Content-Type": "application/json"}}),
|
||||
UsersModule,
|
||||
AuthModule
|
||||
AuthModule,
|
||||
OrganModule,
|
||||
ComplaintModule
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -19,3 +19,11 @@ export enum UserRoleMessages {
|
||||
export enum AuthMessages {
|
||||
TOKEN_EXPIRED_OR_INVALID = 'توکن منسوخ شده یا نامعتبر است!',
|
||||
}
|
||||
|
||||
export enum OrganMessages {
|
||||
ORGAN_NOT_FOUND = 'ارگان مورد نظر پیدا نشد!'
|
||||
}
|
||||
|
||||
export enum ComplaintMessages {
|
||||
COMPLAINT_NOT_FOUND = 'شکایت مورد نظر پیدا نشد!'
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { ComplaintService } from './complaint.service';
|
||||
import { CreateComplaintDto } from './dto/create-complaint.dto';
|
||||
import { UpdateComplaintDto } from './dto/update-complaint.dto';
|
||||
|
||||
@Controller('complaint')
|
||||
export class ComplaintController {
|
||||
constructor(private readonly complaintService: ComplaintService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createComplaintDto: CreateComplaintDto) {
|
||||
return this.complaintService.create(createComplaintDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.complaintService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.complaintService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateComplaintDto: UpdateComplaintDto) {
|
||||
return this.complaintService.update(id, updateComplaintDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.complaintService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ComplaintService } from './complaint.service';
|
||||
import { ComplaintController } from './complaint.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Complaint } from './entities/complaint.entity';
|
||||
import { ComplaintRepository } from './repositories/complaint.repository';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Complaint])],
|
||||
controllers: [ComplaintController],
|
||||
providers: [ComplaintService, ComplaintRepository],
|
||||
})
|
||||
export class ComplaintModule {}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateComplaintDto } from './dto/create-complaint.dto';
|
||||
import { UpdateComplaintDto } from './dto/update-complaint.dto';
|
||||
import { ComplaintRepository } from './repositories/complaint.repository';
|
||||
import { Complaint } from './entities/complaint.entity';
|
||||
import { ComplaintMessages } from 'src/common/enums/messages.enum';
|
||||
|
||||
@Injectable()
|
||||
export class ComplaintService {
|
||||
constructor(private readonly complaintRepository: ComplaintRepository) {}
|
||||
|
||||
async create(createComplaintDto: CreateComplaintDto): Promise<Complaint> {
|
||||
const complaint = this.complaintRepository.create(createComplaintDto);
|
||||
await this.complaintRepository.save(complaint);
|
||||
return complaint;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Complaint[]> {
|
||||
return this.complaintRepository.find();
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<Complaint> {
|
||||
const complaint = await this.complaintRepository.findOne({ where: { id } });
|
||||
if (!complaint)
|
||||
throw new NotFoundException(ComplaintMessages.COMPLAINT_NOT_FOUND);
|
||||
|
||||
return complaint;
|
||||
}
|
||||
|
||||
async update(id: string, updateComplaintDto: UpdateComplaintDto): Promise<Complaint> {
|
||||
const complaint = await this.findOneOrFail(id);
|
||||
Object.assign(complaint, updateComplaintDto);
|
||||
return complaint;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<Complaint> {
|
||||
const complaint = await this.findOneOrFail(id);
|
||||
await this.complaintRepository.remove(complaint);
|
||||
return complaint;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsEnum, IsNotEmpty, IsString, IsUUID } from "class-validator";
|
||||
import { ComplaintStatus } from "../enums/complaint.enum";
|
||||
|
||||
export class CreateComplaintDto {
|
||||
@ApiProperty({
|
||||
example: 'شکایت من',
|
||||
type: 'string'
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
message: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'شکایت من',
|
||||
type: 'string'
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
subject: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: ComplaintStatus.WAITING_FOR_RESPONSE,
|
||||
enum: ComplaintStatus
|
||||
})
|
||||
@IsEnum(ComplaintStatus)
|
||||
status: ComplaintStatus;
|
||||
|
||||
@ApiProperty({example: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', description: 'Id of the User that made the complaint'})
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
userId: string;
|
||||
|
||||
@ApiProperty({example: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', description: 'Id of the Orgain the complaint is about.'})
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
organId: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateComplaintDto } from './create-complaint.dto';
|
||||
|
||||
export class UpdateComplaintDto extends PartialType(CreateComplaintDto) {}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToOne } from 'typeorm';
|
||||
import { ComplaintStatus } from '../enums/complaint.enum';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { Organ } from 'src/organ/entities/organ.entity';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
|
||||
@Entity('complaints')
|
||||
export class Complaint extends BaseEntity {
|
||||
@Column({
|
||||
type: 'text',
|
||||
})
|
||||
message: string;
|
||||
|
||||
@Column({
|
||||
type: 'varchar',
|
||||
length: 100,
|
||||
})
|
||||
subject: string;
|
||||
|
||||
@Column({
|
||||
type: 'enum',
|
||||
enum: ComplaintStatus,
|
||||
default: ComplaintStatus.WAITING_FOR_RESPONSE,
|
||||
})
|
||||
status: ComplaintStatus;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.complaints, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({
|
||||
name: 'userId',
|
||||
})
|
||||
complainant: User;
|
||||
|
||||
@ManyToOne(() => Organ, (organ) => organ.complaints, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
@JoinColumn({
|
||||
name: 'organId'
|
||||
})
|
||||
organ: Organ;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum ComplaintStatus {
|
||||
WAITING_FOR_RESPONSE = 'waiting_for_response',
|
||||
ANSWERED = 'answered'
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Complaint } from '../entities/complaint.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class ComplaintRepository extends Repository<Complaint> {
|
||||
constructor(
|
||||
@InjectRepository(Complaint) complaintRepository: ComplaintRepository,
|
||||
) {
|
||||
super(
|
||||
complaintRepository.target,
|
||||
complaintRepository.manager,
|
||||
complaintRepository.queryRunner,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsString } from "class-validator";
|
||||
|
||||
export class CreateOrganDto {
|
||||
@ApiProperty({example: 'استانداری', type: 'string'})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({example: '/images/logos/xyz.jpg', type: 'string'})
|
||||
@IsString()
|
||||
logo: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateOrganDto } from './create-organ.dto';
|
||||
|
||||
export class UpdateOrganDto extends PartialType(CreateOrganDto) {}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { Complaint } from 'src/complaint/entities/complaint.entity';
|
||||
import { Column, Entity, OneToMany } from 'typeorm';
|
||||
|
||||
@Entity('organs')
|
||||
export class Organ extends BaseEntity {
|
||||
@Column({
|
||||
type: 'varchar',
|
||||
length: 50,
|
||||
})
|
||||
name: string;
|
||||
|
||||
@Column({
|
||||
type: 'varchar',
|
||||
})
|
||||
logo: string;
|
||||
|
||||
@OneToMany(() => Complaint, (complaint) => complaint.organ)
|
||||
complaints: Complaint[];
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { OrganService } from './organ.service';
|
||||
import { CreateOrganDto } from './dto/create-organ.dto';
|
||||
import { UpdateOrganDto } from './dto/update-organ.dto';
|
||||
|
||||
@Controller('organ')
|
||||
export class OrganController {
|
||||
constructor(private readonly organService: OrganService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createOrganDto: CreateOrganDto) {
|
||||
return this.organService.create(createOrganDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.organService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.organService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateOrganDto: UpdateOrganDto) {
|
||||
return this.organService.update(id, updateOrganDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.organService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OrganService } from './organ.service';
|
||||
import { OrganController } from './organ.controller';
|
||||
import { OrganRepository } from './repositories/organ.repository';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Organ } from './entities/organ.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Organ])
|
||||
],
|
||||
controllers: [OrganController],
|
||||
providers: [OrganService, OrganRepository],
|
||||
})
|
||||
export class OrganModule {}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateOrganDto } from './dto/create-organ.dto';
|
||||
import { UpdateOrganDto } from './dto/update-organ.dto';
|
||||
import { OrganRepository } from './repositories/organ.repository';
|
||||
import { OrganMessages } from 'src/common/enums/messages.enum';
|
||||
import { Organ } from './entities/organ.entity';
|
||||
|
||||
@Injectable()
|
||||
export class OrganService {
|
||||
constructor(private readonly organRepository: OrganRepository) {}
|
||||
|
||||
async create(createOrganDto: CreateOrganDto) {
|
||||
const organ = this.organRepository.create(createOrganDto);
|
||||
await this.organRepository.save(organ);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Organ[]> {
|
||||
return await this.organRepository.find();
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<Organ> {
|
||||
const organ = await this.organRepository.findOne({where: {id}});
|
||||
if(!organ) throw new NotFoundException(OrganMessages.ORGAN_NOT_FOUND);
|
||||
|
||||
return organ;
|
||||
}
|
||||
|
||||
async update(id: string, updateOrganDto: UpdateOrganDto) {
|
||||
const organ = await this.findOneOrFail(id);
|
||||
|
||||
Object.assign(organ, updateOrganDto);
|
||||
return await this.organRepository.save(organ);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const organ = await this.findOneOrFail(id);
|
||||
await this.organRepository.remove(organ);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Repository } from "typeorm";
|
||||
import { Organ } from "../entities/organ.entity";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class OrganRepository extends Repository<Organ> {
|
||||
constructor(@InjectRepository(Organ) organRepository: Repository<Organ>) {
|
||||
super(organRepository.target, organRepository.manager, organRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Entity, Column, ManyToMany, JoinTable, OneToMany } from 'typeorm';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { Exclude } from 'class-transformer';
|
||||
import { UserRole } from './user-role.entity';
|
||||
import { Complaint } from 'src/complaint/entities/complaint.entity';
|
||||
|
||||
@Entity('users')
|
||||
export class User extends BaseEntity {
|
||||
@@ -33,4 +34,9 @@ export class User extends BaseEntity {
|
||||
cascade: true
|
||||
})
|
||||
roles: UserRole[];
|
||||
|
||||
@OneToMany(() => Complaint, (complaint) => complaint.complainant, {
|
||||
cascade: true
|
||||
})
|
||||
complaints: Complaint[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user