145 lines
4.2 KiB
TypeScript
145 lines
4.2 KiB
TypeScript
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
|
|
import { Response, Request } from "express";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import { Service } from "typedi";
|
|
import { Prisma } from "@prisma/client";
|
|
import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts";
|
|
import { Subscription } from "le-coffre-resources/dist/Admin";
|
|
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import EmailBuilder from "@Common/emails/EmailBuilder";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class SubscriptionsController extends ApiController {
|
|
constructor(private subscriptionsService: SubscriptionsService, private emailBuilder: EmailBuilder) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all subscriptions
|
|
*/
|
|
@Get("/api/v1/admin/subscriptions", [authHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.SubscriptionsFindManyArgs = {};
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
if (query.where?.uid) {
|
|
this.httpBadRequest(response, "You can't filter by uid");
|
|
return;
|
|
}
|
|
}
|
|
|
|
//call service to get prisma entity
|
|
const subscriptionsEntities = await this.subscriptionsService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const subscriptions = Subscription.hydrateArray<Subscription>(subscriptionsEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, subscriptions);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific subscription by uid
|
|
*/
|
|
@Get("/api/v1/admin/subscriptions/:uid", [authHandler, ruleHandler])
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
//get query
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
const subscriptionEntity = await this.subscriptionsService.getByUid(uid, query);
|
|
|
|
//Hydrate resource with prisma entity
|
|
const subscription = ObjectHydrate.hydrate<Subscription>(new Subscription(), subscriptionEntity!, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, subscription);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Update a subscription
|
|
*/
|
|
@Put("/api/v1/admin/subscriptions/:uid", [authHandler, ruleHandler])
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const subscriptionFound = await this.subscriptionsService.getByUid(uid);
|
|
|
|
if (!subscriptionFound) {
|
|
this.httpNotFoundRequest(response, "subscription not found");
|
|
return;
|
|
}
|
|
|
|
//init Subscription resource with request body values
|
|
const subscriptionEntity = Subscription.hydrate<Subscription>(req.body);
|
|
|
|
//call service to get prisma entity
|
|
const subscriptionEntityUpdated = await this.subscriptionsService.update(uid, subscriptionEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const subscription = Subscription.hydrate<Subscription>(subscriptionEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, subscription);
|
|
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Invite collaborators to a subscription
|
|
*/
|
|
|
|
@Post("/api/v1/admin/subscriptions/invite", [authHandler, ruleHandler])
|
|
protected async inviteCollaborators(req: Request, response: Response) {
|
|
try {
|
|
//get email list from body
|
|
const emails: [string] = req.body.emails;
|
|
if (!emails || emails.length < 1){
|
|
this.httpBadRequest(response, "No emails provided");
|
|
return;
|
|
}
|
|
|
|
//create emails for asked document
|
|
await this.emailBuilder.sendInvitationEmails(emails);
|
|
|
|
//success
|
|
this.httpSuccess(response, {message: "Invitations sent"});
|
|
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|