Managecollaborators
This commit is contained in:
parent
d3b7692823
commit
a590134369
@ -43,6 +43,9 @@ export default class SubscriptionsController extends ApiController {
|
||||
//Hydrate ressource with prisma entity
|
||||
const subscriptions = Subscription.hydrateArray<Subscription>(subscriptionsEntities, { strategy: "excludeAll" });
|
||||
|
||||
console.log(subscriptions[0]?.seats);
|
||||
|
||||
|
||||
//success
|
||||
this.httpSuccess(response, subscriptions);
|
||||
} catch (error) {
|
||||
@ -169,4 +172,43 @@ export default class SubscriptionsController extends ApiController {
|
||||
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;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
@ -70,42 +70,43 @@ export default class UserController extends ApiController {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
} 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) {
|
||||
this.httpUnauthorized(response, "User not subscribed");
|
||||
isSubscribed = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (subscriptions[0]?.type === EType.Unlimited) {
|
||||
if (hasSeat && hasSeat.length > 0) {
|
||||
isSubscribed = true;
|
||||
} else {
|
||||
const hasSeat = await this.subscriptionsService.get({
|
||||
where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } },
|
||||
});
|
||||
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
||||
|
||||
if (hasSeat && hasSeat.length > 0) {
|
||||
isSubscribed = true;
|
||||
} else {
|
||||
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
||||
const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid } });
|
||||
|
||||
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) {
|
||||
const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid);
|
||||
if (seatAdded) {
|
||||
isSubscribed = true;
|
||||
}
|
||||
//if nbMaxSeats < nbCurrentSeats, create a new seat for the user
|
||||
if (nbMaxSeats > nbCurrentSeats.length) {
|
||||
const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid);
|
||||
if (seatAdded) {
|
||||
isSubscribed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (userHydrated.role?.name === "admin") {
|
||||
isSubscribed = true;
|
||||
}
|
||||
|
||||
if (!isSubscribed) {
|
||||
this.httpUnauthorized(response, "User not subscribed");
|
||||
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_uid String @db.VarChar(255)
|
||||
seats Seats[]
|
||||
created_at DateTime? @default(now())
|
||||
updated_at DateTime? @updatedAt
|
||||
@@map("subscriptions")
|
||||
}
|
||||
|
||||
@ -397,6 +399,8 @@ model Seats {
|
||||
subscription_uid String @db.VarChar(255)
|
||||
user Users @relation(fields: [user_uid], references: [uid], onDelete: Cascade)
|
||||
user_uid String @db.VarChar(255)
|
||||
created_at DateTime? @default(now())
|
||||
updated_at DateTime? @updatedAt
|
||||
@@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) {
|
||||
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> {
|
||||
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> {
|
||||
|
||||
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -86,6 +86,24 @@ export default class SubscriptionsRepository extends BaseRepository {
|
||||
* @description : update given subscription
|
||||
*/
|
||||
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")
|
||||
{
|
||||
const updateArgs: Prisma.SubscriptionsUpdateArgs = {
|
||||
|
@ -34,4 +34,12 @@ export default class SeatsService extends BaseService {
|
||||
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 SubscriptionsRepository from "@Repositories/SubscriptionsRepository";
|
||||
import { Subscription } from "le-coffre-resources/dist/Admin";
|
||||
import SeatsService from "../SeatsService/SeatsService";
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionsService extends BaseService {
|
||||
constructor(private subscriptionsRepository: SubscriptionsRepository) {
|
||||
constructor(private subscriptionsRepository: SubscriptionsRepository, private seatsService: SeatsService) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -40,6 +41,17 @@ export default class SubscriptionsService extends BaseService {
|
||||
* @throws {Error} If subscription cannot be modified
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user