delete folder done

This commit is contained in:
Vincent Alamelle 2023-05-09 11:01:42 +02:00
parent 266501fd79
commit 52ac22fe21
3 changed files with 49 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import { Response, Request } from "express";
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { Service } from "typedi";
@ -126,4 +126,29 @@ export default class OfficeFoldersController extends ApiController {
}
this.httpSuccess(response, await this.officeFoldersService.getByUid("uid"));
}
/**
* @description Delete a specific folder
*/
@Delete("/api/v1/super-admin/folders/:uid")
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
//call service to get prisma entity
const officeFoldertEntity: OfficeFolders = await this.officeFoldersService.delete(uid);
//Hydrate ressource with prisma entity
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFoldertEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, officeFolder);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
}

View File

@ -143,4 +143,15 @@ export default class OfficeFoldersRepository extends BaseRepository {
return officeFolderEntity;
}
/**
* @description : Delete a folder
*/
public async delete(uid: string): Promise<OfficeFolders> {
return this.model.delete({
where: {
uid: uid,
},
});
}
}

View File

@ -48,4 +48,16 @@ export default class OfficeFoldersService extends BaseService {
public async getByUid(uid: string, query?: any) {
return this.officeFoldersRepository.findOneByUid(uid, query);
}
/**
* @description : Delete a folder
* @throws {Error} If document cannot be deleted
*/
public async delete(uid: string): Promise<OfficeFolders> {
const officeFolder = await this.officeFoldersRepository.findOneByUid(uid);
if (officeFolder.status !== "ARCHIVED") {
throw new Error("Cannot delete a folder that is not archived");
}
return this.officeFoldersRepository.delete(uid);
}
}