This commit is contained in:
mahyargdz
2025-09-14 15:15:03 +03:30
commit be82059172
534 changed files with 51310 additions and 0 deletions
@@ -0,0 +1,25 @@
import { Expose } from "class-transformer";
import { IsNotEmpty, IsOptional, Length } from "class-validator";
import { ApiProperty } from "../../../common/decorator/swggerDocs";
export class UpdateWarrantyDTO {
@Expose()
@IsOptional()
@IsNotEmpty()
@Length(5, 40)
@ApiProperty({ type: "string", description: "name of the warranty", example: "گارانتی اصالت و سلامت فیزیکی" })
name: string;
@Expose()
@IsOptional()
@IsNotEmpty()
@ApiProperty({ type: "string", description: "duration of the warranty", example: "هفت روز" })
duration: string;
@Expose()
@IsOptional()
@IsNotEmpty()
@ApiProperty({ type: "string", description: "logoUrl of the warranty", example: "https://cdnUrl.com" })
logoUrl: string;
}
@@ -0,0 +1,22 @@
import { Expose } from "class-transformer";
import { IsNotEmpty, Length } from "class-validator";
import { ApiProperty } from "../../../common/decorator/swggerDocs";
export class CreateWarrantyDTO {
@Expose()
@IsNotEmpty()
@Length(5, 40)
@ApiProperty({ type: "string", description: "name of the warranty", example: "گارانتی اصالت و سلامت فیزیکی" })
name: string;
@Expose()
@IsNotEmpty()
@ApiProperty({ type: "string", description: "duration of the warranty", example: "هفت روز" })
duration: string;
@Expose()
@IsNotEmpty()
@ApiProperty({ type: "string", description: "logoUrl of the warranty", example: "https://cdnUrl.com" })
logoUrl: string;
}
+15
View File
@@ -0,0 +1,15 @@
import { Expose } from "class-transformer";
export class WarrantyDTO {
@Expose()
_id: number;
@Expose()
duration: string;
@Expose()
logoUrl: string;
@Expose()
name: string;
}
@@ -0,0 +1,12 @@
import { StatusEnum } from "../../../../common/enums/status.enum";
//TODO:update Warranty field to contain images
export interface IWarranty {
_id: number;
name: string;
status: StatusEnum;
duration: string;
logoUrl: string;
deleted: boolean;
}
@@ -0,0 +1,24 @@
import mongoose, { Schema, model } from "mongoose";
import { IWarranty } from "./Abstraction/IWarranty";
import { StatusEnum } from "../../../common/enums/status.enum";
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const AutoIncrement = require("mongoose-sequence")(mongoose);
const warrantySchema = new Schema<IWarranty>(
{
_id: Number,
name: { type: String, unique: true, required: true },
status: { type: String, enum: StatusEnum, default: StatusEnum.Pending },
duration: { type: String, required: true },
logoUrl: { type: String, required: true },
deleted: { type: Boolean, default: false },
},
{ timestamps: true, toJSON: { versionKey: false }, _id: false },
);
warrantySchema.plugin(AutoIncrement, { id: "warranty_id", inc_field: "_id", start_seq: 100 });
const WarrantyModel = model<IWarranty>("Warranty", warrantySchema);
export { WarrantyModel };
@@ -0,0 +1,70 @@
import { Request } from "express";
import rateLimit from "express-rate-limit";
import { inject } from "inversify";
import { controller, httpGet, httpPost, queryParam, request, requestBody } from "inversify-express-utils";
import { CreateWarrantyDTO } from "./DTO/createWarranty.dto";
import { WarrantyService } from "./warranty.service";
import { HttpStatus } from "../../common";
import { BaseController } from "../../common/base/controller";
import { ApiAuth, ApiFile, ApiModel, ApiOperation, ApiQuery, ApiResponse, ApiTags } from "../../common/decorator/swggerDocs";
import { appConfig } from "../../core/config/app.config";
import { Guard } from "../../core/middlewares/guard.middleware";
import { ValidationMiddleware } from "../../core/middlewares/validator.middleware";
import { IOCTYPES } from "../../IOC/ioc.types";
import { UploadService } from "../../utils/upload.service";
@controller("/warranty")
@ApiTags("Warranty")
class WarrantyController extends BaseController {
@inject(IOCTYPES.WarrantyService) warrantyService: WarrantyService;
@ApiOperation("request to create a new warranty ==> login as seller")
@ApiResponse("successful", HttpStatus.Created)
@ApiModel(CreateWarrantyDTO)
@ApiAuth()
@httpPost("", rateLimit(appConfig.rate), Guard.authSeller(), ValidationMiddleware.validateInput(CreateWarrantyDTO))
public async createWarranty(@requestBody() createWarrantyDto: CreateWarrantyDTO) {
const data = await this.warrantyService.createWarrantyS(createWarrantyDto);
return this.response({ data }, HttpStatus.Created);
}
@ApiOperation("get a list of warranties")
@ApiResponse("successful", HttpStatus.Ok)
@ApiQuery("limit", "the limit of return data")
@ApiQuery("page", "the page want to get")
@ApiQuery("status", "the status of warranties ==> Approved | Pending")
@httpGet("")
public async getAll(@queryParam("limit") limit: string, @queryParam("page") page: string, @queryParam("status") status: string) {
const queries = {
limit: parseInt(limit),
page: parseInt(page),
status,
};
const { count, warranties } = await this.warrantyService.getAllS(queries);
const { pager } = this.paginate(count);
return this.response({ warranties, pager });
}
//uploader
@ApiOperation("Upload a warranty info image ==> need to login as seller")
@ApiResponse("File uploaded successfully", HttpStatus.Accepted)
@ApiFile("image")
@ApiAuth()
@httpPost("/image/upload", Guard.authSeller(), UploadService.single("image", "warranty-images"))
public async uploadImage(@request() req: Request) {
// eslint-disable-next-line no-undef
const file = req.file as Express.MulterS3.File;
const data = {
message: "file uploaded!",
url: {
size: file?.size,
url: file?.location,
type: file?.mimetype,
},
};
return this.response({ data }, HttpStatus.Accepted);
}
}
export { WarrantyController };
@@ -0,0 +1,38 @@
import { IWarranty } from "./models/Abstraction/IWarranty";
import { WarrantyModel } from "./models/warranty.model";
import { BaseRepository } from "../../common/base/repository";
import { StatusEnum } from "../../common/enums/status.enum";
class WarrantyRepository extends BaseRepository<IWarranty> {
constructor() {
super(WarrantyModel);
}
async findAllWarranties(queries: { limit: number; page: number; status: string }) {
const page = queries.page || 1;
const limit = queries.limit || 10;
const skip = (page - 1) * limit;
const query = {
...(queries.status && {
status: queries.status,
}),
deleted: false,
};
const count = await this.model.countDocuments(query);
const docs = await this.model.find(query).skip(skip).limit(limit).sort({ createdAt: -1 });
return {
docs,
count,
};
}
async pendingWarranties() {
return await this.model.countDocuments({ status: StatusEnum.Pending });
}
}
function createWarrantyRepository(): WarrantyRepository {
return new WarrantyRepository();
}
export { WarrantyRepository, createWarrantyRepository };
+66
View File
@@ -0,0 +1,66 @@
import { inject, injectable } from "inversify";
import { CreateWarrantyDTO } from "./DTO/createWarranty.dto";
import { UpdateWarrantyDTO } from "./DTO/UpdateWarranty.dto";
import { WarrantyRepository } from "./warranty.repository";
import { CommonMessage, WarrantyMessage } from "../../common/enums/message.enum";
import { StatusEnum } from "../../common/enums/status.enum";
import { BadRequestError } from "../../core/app/app.errors";
import { IOCTYPES } from "../../IOC/ioc.types";
@injectable()
class WarrantyService {
@inject(IOCTYPES.WarrantyRepository) warrantyRepo: WarrantyRepository;
async createWarrantyS(createDto: CreateWarrantyDTO) {
const existWarr = await this.warrantyRepo.model.exists({ name: createDto.name });
if (existWarr) throw new BadRequestError(WarrantyMessage.DuplicateName);
const newWarranty = await this.warrantyRepo.model.create(createDto);
return {
message: WarrantyMessage.Created,
newWarranty,
};
}
async getAllS(queries: { limit: number; page: number; status: string }) {
const { count, docs } = await this.warrantyRepo.findAllWarranties(queries);
return { warranties: docs, count };
}
async deleteById(id: number) {
const deletedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, { deleted: true }, { new: true });
if (!deletedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
return {
message: CommonMessage.Deleted,
deletedWarranty,
};
}
async updateById(id: number, updateDto: UpdateWarrantyDTO) {
const updatedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, updateDto, { new: true });
if (!updatedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
return {
message: CommonMessage.Updated,
updatedWarranty,
};
}
async approve(id: number) {
const updatedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, { status: StatusEnum.Approved }, { new: true });
if (!updatedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
return {
message: CommonMessage.Updated,
updatedWarranty,
};
}
async getPendingWarranties() {
return await this.warrantyRepo.pendingWarranties();
}
}
export { WarrantyService };