Managecollaborators
This commit is contained in:
parent
d3b7692823
commit
a590134369
@ -41,7 +41,10 @@ export default class SubscriptionsController extends ApiController {
|
|||||||
const subscriptionsEntities = await this.subscriptionsService.get(query);
|
const subscriptionsEntities = await this.subscriptionsService.get(query);
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const subscriptions = Subscription.hydrateArray<Subscription>(subscriptionsEntities, { strategy: "excludeAll" });
|
const subscriptions = Subscription.hydrateArray<Subscription>(subscriptionsEntities, { strategy: "excludeAll" });
|
||||||
|
|
||||||
|
console.log(subscriptions[0]?.seats);
|
||||||
|
|
||||||
|
|
||||||
//success
|
//success
|
||||||
this.httpSuccess(response, subscriptions);
|
this.httpSuccess(response, subscriptions);
|
||||||
@ -169,4 +172,43 @@ export default class SubscriptionsController extends ApiController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * @description Update a subscription seats
|
||||||
|
// */
|
||||||
|
// @Put("/api/v1/admin/subscriptions/:uid/seats", [authHandler, roleHandler])
|
||||||
|
// protected async updateSubscriptionSeats(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 seatEntities = Seat.hydrateArray<Seat>(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;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
@ -69,43 +69,44 @@ export default class UserController extends ApiController {
|
|||||||
this.httpUnauthorized(response, "Email not found");
|
this.httpUnauthorized(response, "Email not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let isSubscribed = false;
|
let isSubscribed = false;
|
||||||
if (userHydrated.role?.name === "admin") {
|
|
||||||
|
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
|
||||||
|
|
||||||
|
if (!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
|
||||||
|
this.httpUnauthorized(response, "User not subscribed");
|
||||||
|
isSubscribed = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subscriptions[0]?.type === EType.Unlimited) {
|
||||||
isSubscribed = true;
|
isSubscribed = true;
|
||||||
} else {
|
} else {
|
||||||
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
|
const hasSeat = await this.subscriptionsService.get({
|
||||||
|
where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } },
|
||||||
|
});
|
||||||
|
|
||||||
if (!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
|
if (hasSeat && hasSeat.length > 0) {
|
||||||
this.httpUnauthorized(response, "User not subscribed");
|
|
||||||
isSubscribed = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (subscriptions[0]?.type === EType.Unlimited) {
|
|
||||||
isSubscribed = true;
|
isSubscribed = true;
|
||||||
} else {
|
} else {
|
||||||
const hasSeat = await this.subscriptionsService.get({
|
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
||||||
where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (hasSeat && hasSeat.length > 0) {
|
const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid } });
|
||||||
isSubscribed = true;
|
|
||||||
} else {
|
|
||||||
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
|
||||||
|
|
||||||
const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid } });
|
//if nbMaxSeats < nbCurrentSeats, create a new seat for the user
|
||||||
|
if (nbMaxSeats > nbCurrentSeats.length) {
|
||||||
//if nbMaxSeats < nbCurrentSeats, create a new seat for the user
|
const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid);
|
||||||
if (nbMaxSeats > nbCurrentSeats.length) {
|
if (seatAdded) {
|
||||||
const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid);
|
isSubscribed = true;
|
||||||
if (seatAdded) {
|
|
||||||
isSubscribed = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (userHydrated.role?.name === "admin") {
|
||||||
|
isSubscribed = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isSubscribed) {
|
if (!isSubscribed) {
|
||||||
this.httpUnauthorized(response, "User not subscribed");
|
this.httpUnauthorized(response, "User not subscribed");
|
||||||
return;
|
return;
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "seats" ADD COLUMN "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
ADD COLUMN "updated_at" TIMESTAMP(3);
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "subscriptions" ADD COLUMN "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
ADD COLUMN "updated_at" TIMESTAMP(3);
|
@ -388,6 +388,8 @@ model Subscriptions {
|
|||||||
office Offices @relation(fields: [office_uid], references: [uid], onDelete: Cascade)
|
office Offices @relation(fields: [office_uid], references: [uid], onDelete: Cascade)
|
||||||
office_uid String @db.VarChar(255)
|
office_uid String @db.VarChar(255)
|
||||||
seats Seats[]
|
seats Seats[]
|
||||||
|
created_at DateTime? @default(now())
|
||||||
|
updated_at DateTime? @updatedAt
|
||||||
@@map("subscriptions")
|
@@map("subscriptions")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -397,6 +399,8 @@ model Seats {
|
|||||||
subscription_uid String @db.VarChar(255)
|
subscription_uid String @db.VarChar(255)
|
||||||
user Users @relation(fields: [user_uid], references: [uid], onDelete: Cascade)
|
user Users @relation(fields: [user_uid], references: [uid], onDelete: Cascade)
|
||||||
user_uid String @db.VarChar(255)
|
user_uid String @db.VarChar(255)
|
||||||
|
created_at DateTime? @default(now())
|
||||||
|
updated_at DateTime? @updatedAt
|
||||||
@@map("seats")
|
@@map("seats")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ export default class SeatsRepository extends BaseRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description : Find many subscriptions
|
* @description : Find many seats
|
||||||
*/
|
*/
|
||||||
public async findMany(query: Prisma.SeatsFindManyArgs) {
|
public async findMany(query: Prisma.SeatsFindManyArgs) {
|
||||||
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
||||||
@ -25,7 +25,7 @@ export default class SeatsRepository extends BaseRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description : find unique subscription
|
* @description : find unique seat
|
||||||
*/
|
*/
|
||||||
public async findOneByUid(uid: string, query?: Prisma.SeatsInclude): Promise<Seats | null> {
|
public async findOneByUid(uid: string, query?: Prisma.SeatsInclude): Promise<Seats | null> {
|
||||||
return this.model.findUnique({
|
return this.model.findUnique({
|
||||||
@ -37,7 +37,7 @@ export default class SeatsRepository extends BaseRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description : Create a subscription
|
* @description : Create a seat
|
||||||
*/
|
*/
|
||||||
public async create(userUid: string, subscriptionUid: string): Promise<Seats> {
|
public async create(userUid: string, subscriptionUid: string): Promise<Seats> {
|
||||||
|
|
||||||
@ -63,5 +63,16 @@ export default class SeatsRepository extends BaseRepository {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description : Delete a seat
|
||||||
|
*/
|
||||||
|
public async delete(uid: string) {
|
||||||
|
return this.model.delete({
|
||||||
|
where: {
|
||||||
|
uid: uid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,25 @@ export default class SubscriptionsRepository extends BaseRepository {
|
|||||||
/**
|
/**
|
||||||
* @description : update given subscription
|
* @description : update given subscription
|
||||||
*/
|
*/
|
||||||
public async update(uid: string, subscription: Subscription): Promise<Subscriptions> {
|
public async update(uid: string, subscription: Subscription): Promise<Subscriptions> {
|
||||||
|
if(!subscription.type || subscription.type === ""){
|
||||||
|
const updateArgs: Prisma.SubscriptionsUpdateArgs = {
|
||||||
|
where: {
|
||||||
|
uid: uid,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
seats:{
|
||||||
|
deleteMany: {},
|
||||||
|
createMany: {
|
||||||
|
data: subscription.seats?.map((seat) => ({
|
||||||
|
user_uid: seat.user.uid || "",
|
||||||
|
})) ?? [],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return this.model.update(updateArgs);
|
||||||
|
}
|
||||||
if(subscription.type === "STANDARD")
|
if(subscription.type === "STANDARD")
|
||||||
{
|
{
|
||||||
const updateArgs: Prisma.SubscriptionsUpdateArgs = {
|
const updateArgs: Prisma.SubscriptionsUpdateArgs = {
|
||||||
|
@ -34,4 +34,12 @@ export default class SeatsService extends BaseService {
|
|||||||
return this.seatsRepository.create(subscriptionUid, userUid);
|
return this.seatsRepository.create(subscriptionUid, userUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description : Delete a seat
|
||||||
|
* @throws {Error} If seat cannot be deleted
|
||||||
|
*/
|
||||||
|
public async delete(uid: string) {
|
||||||
|
return this.seatsRepository.delete(uid);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,11 @@ import { Service } from "typedi";
|
|||||||
import { Prisma, Subscriptions } from "@prisma/client";
|
import { Prisma, Subscriptions } from "@prisma/client";
|
||||||
import SubscriptionsRepository from "@Repositories/SubscriptionsRepository";
|
import SubscriptionsRepository from "@Repositories/SubscriptionsRepository";
|
||||||
import { Subscription } from "le-coffre-resources/dist/Admin";
|
import { Subscription } from "le-coffre-resources/dist/Admin";
|
||||||
|
import SeatsService from "../SeatsService/SeatsService";
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class SubscriptionsService extends BaseService {
|
export default class SubscriptionsService extends BaseService {
|
||||||
constructor(private subscriptionsRepository: SubscriptionsRepository) {
|
constructor(private subscriptionsRepository: SubscriptionsRepository, private seatsService: SeatsService) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +40,18 @@ export default class SubscriptionsService extends BaseService {
|
|||||||
* @description : Modify a subscription
|
* @description : Modify a subscription
|
||||||
* @throws {Error} If subscription cannot be modified
|
* @throws {Error} If subscription cannot be modified
|
||||||
*/
|
*/
|
||||||
public async update(uid: string, subscriptionEntity: Subscription): Promise<Subscriptions> {
|
public async update(uid: string, subscriptionEntity: Subscription): Promise<Subscriptions> {
|
||||||
|
console.log(subscriptionEntity);
|
||||||
|
|
||||||
|
if(subscriptionEntity.type === "STANDARD"){
|
||||||
|
const seats = await this.seatsService.get({ where: { subscription: { uid: uid } }, orderBy: {created_at: 'asc'} });
|
||||||
|
const seatsToKeep = subscriptionEntity.nb_seats;
|
||||||
|
const seatsToDelete = seats.slice(seatsToKeep);
|
||||||
|
|
||||||
|
for (const seat of seatsToDelete) {
|
||||||
|
await this.seatsService.delete(seat.uid);
|
||||||
|
}
|
||||||
|
}
|
||||||
return this.subscriptionsRepository.update(uid, subscriptionEntity);
|
return this.subscriptionsRepository.update(uid, subscriptionEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user