diff --git a/.github/workflows/ppd.yml b/.github/workflows/ppd.yml index 820d214e..29270445 100644 --- a/.github/workflows/ppd.yml +++ b/.github/workflows/ppd.yml @@ -210,7 +210,8 @@ jobs: env_string=$(echo $env_string | sed 's/ $//') scw container container update ${{ env.CONTAINER_ID }} $env_string env: - ENV_VARS: ${{ secrets.ENV_LECOFFRE }} + ENV_VARS: ${{ secrets.ENV }} + SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY_LECOFFRE }} SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY_LECOFFRE }} SCW_DEFAULT_PROJECT_ID: ${{ env.PROJECT_ID_LECOFFRE }} diff --git a/.github/workflows/prd.yml b/.github/workflows/prd.yml index a2d26631..718abfdd 100644 --- a/.github/workflows/prd.yml +++ b/.github/workflows/prd.yml @@ -208,7 +208,8 @@ jobs: env_string=$(echo $env_string | sed 's/ $//') scw container container update ${{ env.CONTAINER_ID }} $env_string env: - ENV_VARS: ${{ secrets.ENV_LECOFFRE }} + ENV_VARS: ${{ secrets.ENV }} + SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY_LECOFFRE }} SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY_LECOFFRE }} SCW_DEFAULT_PROJECT_ID: ${{ env.PROJECT_ID_LECOFFRE }} diff --git a/.github/workflows/stg.yml b/.github/workflows/stg.yml index eb340f7f..bd024bec 100644 --- a/.github/workflows/stg.yml +++ b/.github/workflows/stg.yml @@ -208,8 +208,9 @@ jobs: env_string=$(echo $env_string | sed 's/ $//') scw container container update ${{ env.CONTAINER_ID }} $env_string env: - ENV_VARS: ${{ secrets.ENV_LECOFFRE }} + ENV_VARS: ${{ secrets.ENV }} SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY_LECOFFRE }} SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY_LECOFFRE }} SCW_DEFAULT_PROJECT_ID: ${{ env.PROJECT_ID_LECOFFRE }} - SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_ORGANIZATION_ID_LECOFFRE }} \ No newline at end of file + SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_ORGANIZATION_ID_LECOFFRE }} + diff --git a/Dockerfile-Cron b/Dockerfile-Cron index 3dbfefe4..77780e6f 100644 --- a/Dockerfile-Cron +++ b/Dockerfile-Cron @@ -15,13 +15,6 @@ RUN ssh-keyscan github.com smart-chain-fr/leCoffre-resources.git >> /root/.ssh/k RUN npm install --frozen-lockfile -# Rebuild the source code only when needed -FROM node:19-alpine AS builder - -WORKDIR leCoffre - -COPY --from=deps leCoffre/node_modules ./node_modules -COPY --from=deps leCoffre/package.json package.json COPY tsconfig.json tsconfig.json COPY src src @@ -35,11 +28,12 @@ WORKDIR leCoffre RUN adduser -D lecoffreuser --uid 10000 && chown -R lecoffreuser . -COPY --from=builder --chown=lecoffreuser leCoffre/node_modules ./node_modules -COPY --from=builder --chown=lecoffreuser leCoffre/dist dist -COPY --from=builder --chown=lecoffreuser leCoffre/package.json ./package.json -COPY --from=builder --chown=lecoffreuser leCoffre/src/common/databases ./src/common/databases +COPY --from=deps --chown=lecoffreuser leCoffre/node_modules ./node_modules +COPY --from=deps --chown=lecoffreuser leCoffre/dist dist +COPY --from=deps --chown=lecoffreuser leCoffre/package.json ./package.json +COPY --from=deps --chown=lecoffreuser leCoffre/src/common/databases ./src/common/databases +RUN apk update && apk add chromium USER lecoffreuser CMD ["npm", "run", "cron"] diff --git a/devops/ppd.values.yaml b/devops/ppd.values.yaml index 855ab153..78ece8a3 100644 --- a/devops/ppd.values.yaml +++ b/devops/ppd.values.yaml @@ -1,4 +1,4 @@ -dockerPullSecret: docker-pull-secret +-tpdockerPullSecret: docker-pull-secret scwSecretKey: AgChoEnPitXp4Ny/rVMEcevaWKNVpyj2cJYAcq+yFqKwVwnLB+ffDvwqz9XBHu+6d4Nyyjkf37zUAMoaM21lEDWA7x3zfG2/D/j+rvX1qxzZgLD0mjBk7fGElVm332I6JA83oInes8AMMYEDPLElzHnpKRb9KtkIP4NzgOcCeW0ijft3N7Vroez6LEHsBPCA1I9XjKSkGEDvrO0MhWX3iJOlfz+SPMfJAV7rPawOs0ZmohTHrPW8qIvGDn8HCzKyU8zRBoMt+Ogpf5pH4U3JryEFuqD61KAQgablAM8edPIvsgNno9HAEuC2QtRLYA9aUhuKdaKuS58c9P2E80PHWXIlbpFCg6EugQTgNfnYp+3qDUNz8edeCfapYLvF4s9eCMGyMsGnpDR8EDNOyuGy7Y3l7okX8Xqu464gMp9E+hX7bHkcD6a4xfyIgJcWxsku0tm1TH1dpn4M1UXRuyZZif8P08nuE6MTUL67sAR9J1lpn4lVEL4kflk0pP2tZ5ncgPQFafJrRz05krMb0eU5tb2H4gs7ao/LL6idWo8MM9K1yr8lIuT5x2WW5CX+RjA+i50ex114V6vX3PNP5oVyt+DynTUB9QmXzVm3oLfDc3Cae1uqh7X0CFd+xiztJBtg0VtJaD/xUJcuWfY4cV2lERo9fRrykltzlJqiXHO4nowt8OtN0BcViVV8NJhPhYFzyb4ympxpOlTjm3GETuT2TYhUqdgS9nzleEAbOmOHZdIO2COunPE= @@ -18,10 +18,10 @@ lecoffreBack: limits: memory: 2Gi ingress: - host: api.ppd.lecoffre.smart-chain.fr + host: api-tp.ppd.lecoffre.smart-chain.fr tls: hosts: - - api.ppd.lecoffre.smart-chain.fr + - api-tp.ppd.lecoffre.smart-chain.fr secretName: api-tls annotations: kubernetes.io/ingress.class: nginx diff --git a/package.json b/package.json index a6f832c1..5afa8195 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "file-type-checker": "^1.0.8", "fp-ts": "^2.16.1", "jsonwebtoken": "^9.0.0", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.119", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.132", "module-alias": "^2.2.2", "monocle-ts": "^2.3.13", "multer": "^1.4.5-lts.1", @@ -70,6 +70,7 @@ "prisma-query": "^2.0.0", "puppeteer": "^21.3.4", "reflect-metadata": "^0.1.13", + "stripe": "^14.22.0", "ts-node": "^10.9.1", "tslib": "^2.4.1", "typedi": "^0.10.0", diff --git a/src/app/api/admin/OfficeRolesController.ts b/src/app/api/admin/OfficeRolesController.ts index cb3162bd..29cec7ad 100644 --- a/src/app/api/admin/OfficeRolesController.ts +++ b/src/app/api/admin/OfficeRolesController.ts @@ -29,7 +29,7 @@ export default class OfficeRolesController extends ApiController { let query: Prisma.OfficeRolesFindManyArgs = {}; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); - if(query.where?.uid) { + if (query.where?.uid) { this.httpBadRequest(response, "You can't filter by uid"); return; } @@ -75,6 +75,17 @@ export default class OfficeRolesController extends ApiController { //init IOfficeRole resource with request body values const officeRoleEntity = OfficeRole.hydrate(req.body); + const allRules = await this.rulesService.get({ + where: { + OR: [ + { + namespace: "global", + }, + ], + }, + }); + + officeRoleEntity.rules = allRules; //validate officeRole await validateOrReject(officeRoleEntity, { groups: ["createOfficeRole"] }); @@ -113,24 +124,27 @@ export default class OfficeRolesController extends ApiController { return; } - if (req.body.rules) { + if (req.body.rules) { const allRules = await this.rulesService.get({ where: { - OR: [ - { - namespace: "notary", - }, - { - namespace: "collaborator", - }, - ], + namespace: "global", + }, + }); + + const specificRules = await this.rulesService.get({ + where: { + namespace: "collaborator", }, }); req.body.rules = req.body.rules.filter((rule: any) => { - const ruleFound = allRules.find((r) => r.uid === rule.uid && (r.namespace === "notary" || r.namespace === "collaborator")); + const ruleFound = specificRules.find( + (r) => r.uid === rule.uid && (r.namespace === "collaborator"), + ); return ruleFound; }); + + req.body.rules = req.body.rules.concat(allRules); } //init IOfficeRole resource with request body values const officeRoleEntity = OfficeRole.hydrate(req.body); diff --git a/src/app/api/admin/RulesGroupsController.ts b/src/app/api/admin/RulesGroupsController.ts new file mode 100644 index 00000000..0d691fe2 --- /dev/null +++ b/src/app/api/admin/RulesGroupsController.ts @@ -0,0 +1,49 @@ +import { Controller, Get} 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 { RulesGroup } from "le-coffre-resources/dist/Admin"; +import roleHandler from "@App/middlewares/RolesHandler"; +import authHandler from "@App/middlewares/AuthHandler"; +import RulesGroupsService from "@Services/admin/RulesGroupsService/RulesGroupsService"; + +@Controller() +@Service() +export default class RulesRoupsController extends ApiController { + constructor(private rulesGroupsService: RulesGroupsService) { + super(); + } + + /** + * @description Get all subscriptions + */ + @Get("/api/v1/admin/rules-groups", [authHandler, roleHandler] ) + protected async get(req: Request, response: Response) { + try { + //get query + let query: Prisma.RulesGroupsFindManyArgs = {}; + 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 rulesGroupsEntities = await this.rulesGroupsService.get(query); + + //Hydrate ressource with prisma entity + const rulesGroups = RulesGroup.hydrateArray(rulesGroupsEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rulesGroups); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + +} diff --git a/src/app/api/admin/StripeController.ts b/src/app/api/admin/StripeController.ts new file mode 100644 index 00000000..9f3338bb --- /dev/null +++ b/src/app/api/admin/StripeController.ts @@ -0,0 +1,98 @@ +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +// import roleHandler from "@App/middlewares/RolesHandler"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Controller, Get, Post} from "@ControllerPattern/index"; +import StripeService from "@Services/common/StripeService/StripeService"; +import { validateOrReject } from "class-validator"; +import { Request, Response } from "express"; +import { Subscription } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Controller() +@Service() +export default class StripeController extends ApiController { + constructor(private stripeService: StripeService) { + super(); + } + + /** + * @description Create a new checkout session + */ + @Post("/api/v1/admin/stripe", [authHandler, ruleHandler]) + protected async createStripeSubscriptionCheckout(req: Request, response: Response) { + try { + const officeId: string = req.body.user.office_Id; + + //add office id to request body + req.body.office = { uid: officeId }; + + //init Subscription resource with request body values + const subscriptionEntity = Subscription.hydrate(req.body, { strategy: "excludeAll" }); + + await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); + + const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); + + this.httpCreated(response, stripeSession); + + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + @Get("/api/v1/admin/stripe/:uid") + protected async getStripeSubscriptionByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const stripe_subscription = await this.stripeService.getStripeSubscriptionByUid(uid); + + this.httpSuccess(response, stripe_subscription); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + @Get("/api/v1/admin/stripe/:uid/client-portal", [authHandler, ruleHandler]) + protected async getClientPortalSession(req: Request, response: Response) { + try { + + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const client_portal = await this.stripeService.createClientPortalSession(uid); + + this.httpSuccess(response, client_portal); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + @Get("/api/v1/admin/stripe/:uid/customer") + protected async getCustomerBySubscription(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const customer = await this.stripeService.getCustomerBySubscription(uid); + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/SubscriptionsController.ts b/src/app/api/admin/SubscriptionsController.ts new file mode 100644 index 00000000..c0f49241 --- /dev/null +++ b/src/app/api/admin/SubscriptionsController.ts @@ -0,0 +1,144 @@ +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(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(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(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(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; + } + } +} diff --git a/src/app/api/idnot/OfficeController.ts b/src/app/api/idnot/OfficeController.ts index 3e00e7bf..32afb042 100644 --- a/src/app/api/idnot/OfficeController.ts +++ b/src/app/api/idnot/OfficeController.ts @@ -3,6 +3,8 @@ import { Controller, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import IdNotService from "@Services/common/IdNotService/IdNotService"; +import userHandler from "@App/middlewares/OfficeMembershipHandlers/UserHandler"; +import authHandler from "@App/middlewares/AuthHandler"; @Controller() @Service() @@ -11,16 +13,17 @@ export default class UserController extends ApiController { super(); } - @Get("/api/v1/idnot/office/:uid/office-memberships") + @Get("/api/v1/idnot/office/:uid/office-memberships", [authHandler, userHandler]) protected async getOfficeMemberships(req: Request, response: Response) { - try { + try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "uid is required"); return; } - const officeMemberships = await this.idNotService.getOfficeMemberships(uid); + const officeMemberships = await this.idNotService.getOfficeMemberships(uid); + this.httpSuccess(response, officeMemberships); } catch (error) { console.log(error); diff --git a/src/app/api/idnot/UserController.ts b/src/app/api/idnot/UserController.ts index 0521a747..0b8fec33 100644 --- a/src/app/api/idnot/UserController.ts +++ b/src/app/api/idnot/UserController.ts @@ -2,17 +2,28 @@ import { Response, Request } from "express"; import { Controller, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; -import AuthService, { IUserJwtPayload } from "@Services/common/AuthService/AuthService"; +import AuthService, { IUserJwtPayload, PROVIDER_OPENID } from "@Services/common/AuthService/AuthService"; import IdNotService from "@Services/common/IdNotService/IdNotService"; -import WhitelistService from "@Services/common/WhitelistService/WhitelistService"; -import User from "le-coffre-resources/dist/SuperAdmin"; +import User, { RulesGroup } from "le-coffre-resources/dist/Admin"; import UsersService from "@Services/super-admin/UsersService/UsersService"; +import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts"; +import { ESubscriptionStatus } from "@prisma/client"; +import SeatsService from "@Services/admin/SeatsService/SeatsService"; +import { EType } from "le-coffre-resources/dist/Admin/Subscription"; +import RulesGroupsService from "@Services/admin/RulesGroupsService/RulesGroupsService"; @Controller() @Service() export default class UserController extends ApiController { - constructor(private authService: AuthService, private idNotService: IdNotService, private whitelistService: WhitelistService, private userService: UsersService) { + constructor( + private authService: AuthService, + private idNotService: IdNotService, + private userService: UsersService, + private subscriptionsService: SubscriptionsService, + private seatsService: SeatsService, + private rulesGroupsService: RulesGroupsService, + ) { super(); } @@ -25,59 +36,108 @@ export default class UserController extends ApiController { protected async getUserInfosFromIdnot(req: Request, response: Response) { try { const code = req.params["code"]; - - if (!code) throw new Error("code is required"); + + if (!code) throw new Error("code is required"); const idNotToken = await this.idNotService.getIdNotToken(code); - - if(!idNotToken) { + + if (!idNotToken) { this.httpValidationError(response, "IdNot token undefined"); return; } - const user = await this.idNotService.getOrCreateUser(idNotToken); + const user = await this.idNotService.getOrCreateUser(idNotToken); - if(!user) { - this.httpUnauthorized(response, "Email not found"); + if (!user) { + this.httpUnauthorized(response, "User not found"); return; } - await this.idNotService.updateUser(user.uid); + await this.idNotService.updateUser(user.uid); //Whitelist feature //Get user with contact - const prismaUser = await this.userService.getByUid(user.uid, {contact: true }); - + const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true, office_membership: true}); + if (!prismaUser) { this.httpNotFoundRequest(response, "user not found"); return; } - - //Hydrate user to be able to use his contact - const userHydrated = User.hydrate(prismaUser, { strategy: "excludeAll" }); - if(!userHydrated.contact?.email || userHydrated.contact?.email === "") { + //Hydrate user to be able to use his contact + const userHydrated = User.hydrate(prismaUser, { strategy: "excludeAll" }); + + if (!userHydrated.contact?.email || userHydrated.contact?.email === "") { this.httpUnauthorized(response, "Email not found"); return; } + let isSubscribed = false; + + const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } }); + + if (!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) { + isSubscribed = false; + } + else if (subscriptions[0]?.type === EType.Unlimited) { + isSubscribed = true; + } else { + const hasSeat = await this.subscriptionsService.get({ + where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } }, + }); + + 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 } }); + + //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; + } + } + } + } //Check if user is whitelisted - - const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email); + // const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email); + + //When we'll switch to idNotId whitelisting + // const isWhitelisted = await this.userWhitelistService.getByIdNotId(user.idNot); //If not whitelisted, return 409 Not whitelisted - if (!isWhitelisted || isWhitelisted.length === 0) { - this.httpNotWhitelisted(response); + // if (!isWhitelisted || isWhitelisted.length === 0) { + // this.httpNotWhitelisted(response); + // return; + // } + + await this.idNotService.updateOffice(user.office_uid); + + const payload = await this.authService.getUserJwtPayload(user.idNot); + if(!payload) return; + + if(!isSubscribed && userHydrated.role?.name === "admin" || userHydrated.role?.name === "super-admin"){ + const manageSubscriptionRulesEntity = await this.rulesGroupsService.get({ where: { uid: "94343601-04c8-44ef-afb9-3047597528a9" }, include: { rules: true } }); + + const manageSubscriptionRules = RulesGroup.hydrateArray(manageSubscriptionRulesEntity, { strategy: "excludeAll" }); + if(!manageSubscriptionRules[0]) return; + + payload.rules = manageSubscriptionRules[0].rules!.map((rule) => rule.name) || []; + + isSubscribed = true; + } + + if (!isSubscribed) { + this.httpUnauthorized(response, "User not subscribed"); return; } - - await this.idNotService.updateOffice(user.office_uid); - - const payload = await this.authService.getUserJwtPayload(user.idNot); const accessToken = this.authService.generateAccessToken(payload); const refreshToken = this.authService.generateRefreshToken(payload); - + this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); @@ -98,21 +158,24 @@ export default class UserController extends ApiController { } let accessToken; - this.authService.verifyRefreshToken(token, (err, userPayload) => { + this.authService.verifyRefreshToken(token, async (err, userPayload) => { if (err) { console.log(err); this.httpUnauthorized(response); return; } - const user = userPayload as IUserJwtPayload; + const openId = (userPayload as IUserJwtPayload).openId.userId; + if (!openId) return; + const newUserPayload = await this.authService.getUserJwtPayload(openId.toString(), PROVIDER_OPENID.idNot); + const user = newUserPayload as IUserJwtPayload; delete user.iat; delete user.exp; accessToken = this.authService.generateAccessToken(user); + this.httpSuccess(response, { accessToken }); }); //success - this.httpSuccess(response, { accessToken }); } catch (error) { console.log(error); this.httpInternalError(response); diff --git a/src/app/api/notary/OfficeRibController.ts b/src/app/api/notary/OfficeRibController.ts index 3a0fb9ae..035919e4 100644 --- a/src/app/api/notary/OfficeRibController.ts +++ b/src/app/api/notary/OfficeRibController.ts @@ -7,6 +7,7 @@ import OfficerRibService from "@Services/common/OfficeRibService/OfficeRibServic import authHandler from "@App/middlewares/AuthHandler"; import OfficesService from "@Services/notary/OfficesService/OfficesService"; import { Office as OfficeResource } from "le-coffre-resources/dist/Notary"; +import ruleHandler from "@App/middlewares/RulesHandler"; @Controller() @Service() @@ -15,7 +16,7 @@ export default class OfficeRibController extends ApiController { super(); } - @Get("/api/v1/notary/office/rib", [authHandler]) + @Get("/api/v1/notary/rib", [authHandler, ruleHandler]) protected async getRibStream(req: Request, response: Response) { const officeId: string = req.body.user.office_Id; if (!officeId) throw new Error("No officeId provided"); @@ -43,7 +44,7 @@ export default class OfficeRibController extends ApiController { } } - @Post("/api/v1/notary/office/rib", [authHandler]) + @Post("/api/v1/notary/rib", [authHandler, ruleHandler]) protected async post(req: Request, response: Response) { try { const officeId: string = req.body.user.office_Id; @@ -82,7 +83,7 @@ export default class OfficeRibController extends ApiController { } } - @Delete("/api/v1/notary/office/rib", [authHandler]) + @Delete("/api/v1/notary/rib", [authHandler, ruleHandler]) protected async delete(req: Request, response: Response) { try { const officeId: string = req.body.user.office_Id; diff --git a/src/app/index.ts b/src/app/index.ts index 20343be2..1590b6f1 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -49,6 +49,11 @@ import AuthController from "./api/customer/AuthController"; import NotaryOfficeRibController from "./api/notary/OfficeRibController"; import CustomerOfficeRibController from "./api/customer/OfficeRibController"; import IdNotOfficeController from "./api/idnot/OfficeController"; +import SubscriptionsController from "./api/admin/SubscriptionsController"; +import StripeController from "./api/admin/StripeController"; +import StripeWebhooks from "@Common/webhooks/stripeWebhooks"; +import RulesGroupsController from "./api/admin/RulesGroupsController"; + /** * @description This allow to declare all controllers used in the application */ @@ -104,6 +109,10 @@ export default { Container.get(AuthController); Container.get(NotaryOfficeRibController); Container.get(CustomerOfficeRibController); - Container.get(IdNotOfficeController) + Container.get(IdNotOfficeController); + Container.get(SubscriptionsController); + Container.get(StripeController); + Container.get(StripeWebhooks); + Container.get(RulesGroupsController); }, }; diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index 93a426b2..ac3179db 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -142,6 +142,24 @@ export class BackendVariables { @IsNotEmpty() public readonly SCW_BUCKET_NAME!: string; + @IsNotEmpty() + public readonly STRIPE_SECRET_KEY!: string; + + @IsNotEmpty() + public readonly STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID!: string; + + @IsNotEmpty() + public readonly STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID!: string; + + @IsNotEmpty() + public readonly STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID!: string; + + @IsNotEmpty() + public readonly STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID!: string; + + @IsNotEmpty() + public readonly IDNOT_PROD_BASE_URL!: string; + public constructor() { dotenv.config(); this.DATABASE_PORT = process.env["DATABASE_PORT"]!; @@ -190,6 +208,12 @@ export class BackendVariables { this.SCW_ACCESS_KEY_SECRET = process.env["ACCESS_KEY_SECRET"]!; this.SCW_BUCKET_ENDPOINT = process.env["BUCKET_ENDPOINT"]!; this.SCW_BUCKET_NAME = process.env["BUCKET_NAME"]!; + this.STRIPE_SECRET_KEY = process.env["STRIPE_SECRET_KEY"]!; + this.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID"]!; + this.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID"]!; + this.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID"]!; + this.STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID"]!; + this.IDNOT_PROD_BASE_URL = process.env["IDNOT_PROD_BASE_URL"]!; } public async validate(groups?: string[]) { const validationOptions = groups ? { groups } : undefined; diff --git a/src/common/databases/migrations/20240313090933_whitelist_by_idnot_id/migration.sql b/src/common/databases/migrations/20240313090933_whitelist_by_idnot_id/migration.sql new file mode 100644 index 00000000..5581a06e --- /dev/null +++ b/src/common/databases/migrations/20240313090933_whitelist_by_idnot_id/migration.sql @@ -0,0 +1,16 @@ +-- CreateTable +CREATE TABLE "user_whitelist" ( + "uid" TEXT NOT NULL, + "idNot" VARCHAR(255) NOT NULL, + "active" BOOLEAN NOT NULL DEFAULT true, + "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3), + + CONSTRAINT "user_whitelist_pkey" PRIMARY KEY ("uid") +); + +-- CreateIndex +CREATE UNIQUE INDEX "user_whitelist_uid_key" ON "user_whitelist"("uid"); + +-- CreateIndex +CREATE UNIQUE INDEX "user_whitelist_idNot_key" ON "user_whitelist"("idNot"); diff --git a/src/common/databases/migrations/20240325150228_subscriptions/migration.sql b/src/common/databases/migrations/20240325150228_subscriptions/migration.sql new file mode 100644 index 00000000..8319eafb --- /dev/null +++ b/src/common/databases/migrations/20240325150228_subscriptions/migration.sql @@ -0,0 +1,34 @@ +-- CreateTable +CREATE TABLE "subscriptions" ( + "uid" TEXT NOT NULL, + "start_date" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP, + "end_date" TIMESTAMP(3), + "nb_seats" INTEGER NOT NULL, + "office_uid" VARCHAR(255) NOT NULL, + + CONSTRAINT "subscriptions_pkey" PRIMARY KEY ("uid") +); + +-- CreateTable +CREATE TABLE "seats" ( + "uid" TEXT NOT NULL, + "subscription_uid" VARCHAR(255) NOT NULL, + "user_uid" VARCHAR(255) NOT NULL, + + CONSTRAINT "seats_pkey" PRIMARY KEY ("uid") +); + +-- CreateIndex +CREATE UNIQUE INDEX "subscriptions_uid_key" ON "subscriptions"("uid"); + +-- CreateIndex +CREATE UNIQUE INDEX "seats_uid_key" ON "seats"("uid"); + +-- AddForeignKey +ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "seats" ADD CONSTRAINT "seats_subscription_uid_fkey" FOREIGN KEY ("subscription_uid") REFERENCES "subscriptions"("uid") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "seats" ADD CONSTRAINT "seats_user_uid_fkey" FOREIGN KEY ("user_uid") REFERENCES "users"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/src/common/databases/migrations/20240327160202_edit_subscriptions/migration.sql b/src/common/databases/migrations/20240327160202_edit_subscriptions/migration.sql new file mode 100644 index 00000000..f460f2f5 --- /dev/null +++ b/src/common/databases/migrations/20240327160202_edit_subscriptions/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - Added the required column `priceId` to the `subscriptions` table without a default value. This is not possible if the table is not empty. + - Made the column `start_date` on table `subscriptions` required. This step will fail if there are existing NULL values in that column. + - Made the column `end_date` on table `subscriptions` required. This step will fail if there are existing NULL values in that column. + +*/ +-- AlterTable +ALTER TABLE "subscriptions" ADD COLUMN "priceId" VARCHAR(255) NOT NULL, +ALTER COLUMN "start_date" SET NOT NULL, +ALTER COLUMN "end_date" SET NOT NULL; diff --git a/src/common/databases/migrations/20240328104442_subscription_type/migration.sql b/src/common/databases/migrations/20240328104442_subscription_type/migration.sql new file mode 100644 index 00000000..6931a9db --- /dev/null +++ b/src/common/databases/migrations/20240328104442_subscription_type/migration.sql @@ -0,0 +1,11 @@ +/* + Warnings: + + - Added the required column `type` to the `subscriptions` table without a default value. This is not possible if the table is not empty. + +*/ +-- CreateEnum +CREATE TYPE "ESubscriptionType" AS ENUM ('STANDARD', 'UNLIMITED'); + +-- AlterTable +ALTER TABLE "subscriptions" ADD COLUMN "type" "ESubscriptionType" NOT NULL; diff --git a/src/common/databases/migrations/20240329094447_stripe_subscription_id/migration.sql b/src/common/databases/migrations/20240329094447_stripe_subscription_id/migration.sql new file mode 100644 index 00000000..02e49af0 --- /dev/null +++ b/src/common/databases/migrations/20240329094447_stripe_subscription_id/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `priceId` on the `subscriptions` table. All the data in the column will be lost. + - Added the required column `stripe_subscription_id` to the `subscriptions` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "subscriptions" DROP COLUMN "priceId", +ADD COLUMN "stripe_subscription_id" VARCHAR(255) NOT NULL; diff --git a/src/common/databases/migrations/20240329095902_subscription_status/migration.sql b/src/common/databases/migrations/20240329095902_subscription_status/migration.sql new file mode 100644 index 00000000..8c9d5f83 --- /dev/null +++ b/src/common/databases/migrations/20240329095902_subscription_status/migration.sql @@ -0,0 +1,5 @@ +-- CreateEnum +CREATE TYPE "ESubscriptionStatus" AS ENUM ('ACTIVE', 'INACTIVE'); + +-- AlterTable +ALTER TABLE "subscriptions" ADD COLUMN "status" "ESubscriptionStatus" NOT NULL DEFAULT 'INACTIVE'; diff --git a/src/common/databases/migrations/20240329135543_subscription_status_default_active/migration.sql b/src/common/databases/migrations/20240329135543_subscription_status_default_active/migration.sql new file mode 100644 index 00000000..e036e137 --- /dev/null +++ b/src/common/databases/migrations/20240329135543_subscription_status_default_active/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "subscriptions" ALTER COLUMN "status" SET DEFAULT 'ACTIVE'; diff --git a/src/common/databases/migrations/20240408143448_subscription_seats_date/migration.sql b/src/common/databases/migrations/20240408143448_subscription_seats_date/migration.sql new file mode 100644 index 00000000..d66ecc67 --- /dev/null +++ b/src/common/databases/migrations/20240408143448_subscription_seats_date/migration.sql @@ -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); diff --git a/src/common/databases/migrations/20240417122422_rules_groups/migration.sql b/src/common/databases/migrations/20240417122422_rules_groups/migration.sql new file mode 100644 index 00000000..77df62a3 --- /dev/null +++ b/src/common/databases/migrations/20240417122422_rules_groups/migration.sql @@ -0,0 +1,30 @@ +-- CreateTable +CREATE TABLE "rules_groups" ( + "uid" TEXT NOT NULL, + "name" VARCHAR(255) NOT NULL, + "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3), + + CONSTRAINT "rules_groups_pkey" PRIMARY KEY ("uid") +); + +-- CreateTable +CREATE TABLE "_RulesGroupsHasRules" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "rules_groups_uid_key" ON "rules_groups"("uid"); + +-- CreateIndex +CREATE UNIQUE INDEX "_RulesGroupsHasRules_AB_unique" ON "_RulesGroupsHasRules"("A", "B"); + +-- CreateIndex +CREATE INDEX "_RulesGroupsHasRules_B_index" ON "_RulesGroupsHasRules"("B"); + +-- AddForeignKey +ALTER TABLE "_RulesGroupsHasRules" ADD CONSTRAINT "_RulesGroupsHasRules_A_fkey" FOREIGN KEY ("A") REFERENCES "rules"("uid") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_RulesGroupsHasRules" ADD CONSTRAINT "_RulesGroupsHasRules_B_fkey" FOREIGN KEY ("B") REFERENCES "rules_groups"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/src/common/databases/schema.prisma b/src/common/databases/schema.prisma index 53858e86..3d813215 100644 --- a/src/common/databases/schema.prisma +++ b/src/common/databases/schema.prisma @@ -68,6 +68,7 @@ model Users { appointment Appointments[] votes Votes[] user_notifications UserNotifications[] + seats Seats[] @@map("users") } @@ -81,6 +82,15 @@ model Whitelist { @@map("whitelist") } +model UserWhitelist { + uid String @id @unique @default(uuid()) + idNot String @unique @db.VarChar(255) + active Boolean @default(true) + created_at DateTime? @default(now()) + updated_at DateTime? @updatedAt + @@map("user_whitelist") +} + model Offices { uid String @id @unique @default(uuid()) idNot String @unique @db.VarChar(255) @@ -99,6 +109,7 @@ model Offices { office_folders OfficeFolders[] document_types DocumentTypes[] office_roles OfficeRoles[] + subscriptions Subscriptions[] @@map("offices") } @@ -312,9 +323,19 @@ model Rules { role Roles[] @relation("RolesHasRules") office_roles OfficeRoles[] @relation("OfficeRolesHasRules") namespace String @db.VarChar(255) @default("notary") + groups RulesGroups[] @relation("RulesGroupsHasRules") @@map("rules") } +model RulesGroups { + uid String @id @unique @default(uuid()) + name String @db.VarChar(255) + created_at DateTime? @default(now()) + updated_at DateTime? @updatedAt + rules Rules[] @relation("RulesGroupsHasRules") + @@map("rules_groups") +} + model Emails { uid String @id @unique @default(uuid()) templateName String @db.VarChar(255) @@ -366,6 +387,43 @@ model TotpCodes { @@map("totp_codes") } +model Subscriptions { + uid String @id @unique @default(uuid()) + type ESubscriptionType + status ESubscriptionStatus @default(ACTIVE) + stripe_subscription_id String @db.VarChar(255) + start_date DateTime @default(now()) + end_date DateTime + nb_seats Int + 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") +} + +model Seats { + uid String @id @unique @default(uuid()) + subscription Subscriptions @relation(fields: [subscription_uid], references: [uid], onDelete: Cascade) + 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") +} + +enum ESubscriptionStatus { + ACTIVE + INACTIVE +} + +enum ESubscriptionType { + STANDARD + UNLIMITED +} + enum TotpCodesReasons { LOGIN RESET_PASSWORD diff --git a/src/common/databases/seeders/seeder.ts b/src/common/databases/seeders/seeder.ts index d8f1339c..ce7da8bb 100644 --- a/src/common/databases/seeders/seeder.ts +++ b/src/common/databases/seeders/seeder.ts @@ -527,28 +527,28 @@ export default async function main() { label: "Lecture des utilisateurs", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET offices", label: "Afficher des offices", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET customers", label: "Afficher des clients", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET deeds", label: "Voir des types d'acte", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET deed-types", @@ -562,7 +562,7 @@ export default async function main() { label: "Afficher des documents", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET document-types", @@ -576,112 +576,112 @@ export default async function main() { label: "Lecture des fichiers", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET folders", label: "Afficher les dossiers", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET roles", label: "Afficher les rôles", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET rules", label: "Afficher les droits", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "GET office-roles", label: "Lecture des rôles d'office", created_at: new Date(), updated_at: new Date(), - namespace: "collaborator", + namespace: "global", }, { name: "POST deeds", label: "Créer un template de type d'acte", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "collaborator", }, { name: "PUT deeds", label: "Modifier un type d'acte", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "collaborator", }, { name: "DELETE deeds", label: "Supprimer des types d'actes", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "collaborator", }, { name: "POST folders", label: "Créer un dossier", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "PUT folders", label: "Modifier des dossiers", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "DELETE folders", label: "Supprimer un dossier vide", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "POST documents", label: "Demander des documents à un client", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "PUT documents", label: "Valider des documents", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "DELETE documents", label: "Supprimer un document demandé", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "POST customers", label: "Créer des clients", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "PUT customers", label: "Modifier des clients", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "DELETE customers", @@ -695,56 +695,56 @@ export default async function main() { label: "Ancrer un dossier", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "GET anchors", label: "Vérifier l'ancrage un dossier", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "global", }, { name: "POST deed-types", label: "Création des types d'actes", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "PUT deed-types", label: "Modification des types d'actes", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "DELETE deed-types", label: "Suppression des types d'actes", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "POST document-types", label: "Création des types de documents", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "PUT document-types", label: "Modification des types de documents", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "DELETE document-types", label: "Suppression des types de documents", created_at: new Date(), updated_at: new Date(), - namespace: "admin", + namespace: "collaborator", }, { name: "POST office-roles", @@ -793,13 +793,115 @@ export default async function main() { label: "Editer le RIB de l'office", created_at: new Date(), updated_at: new Date(), - namespace: "notary", + namespace: "collaborator", + }, + { + name: "GET rib", + label: "Lire le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "POST rib", + label: "Déposer le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "DELETE rib", + label: "Supprimer le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET subscriptions", + label: "Récupérer les abonnements", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "POST subscriptions", + label: "Inviter un collaborateur à l'abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "PUT subscriptions", + label: "Modifier l'abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET stripe", + label: "Gérer l'abonnement de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "POST stripe", + label: "Payer un abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + ]; + + const rulesGroups = [ + { + name: "Gestion des matrices d'actes et des documents", + created_at: new Date(), + updated_at: new Date(), + rules: [ + "POST deeds", + "PUT deeds", + "DELETE deeds", + "GET document-types", + "DELETE deed-types", + "PUT deed-types", + "DELETE document-types", + "GET deed-types", + "POST document-types", + "POST deed-types", + "PUT document-types", + ] + }, + { + name: "Gestion de l'abonnement", + uid: "94343601-04c8-44ef-afb9-3047597528a9", + created_at: new Date(), + updated_at: new Date(), + rules : [ + "GET subscriptions", + "POST subscriptions", + "PUT subscriptions", + "GET stripe", + "POST stripe", + ] + }, + { + name: "Intégration du RIB", + created_at: new Date(), + updated_at: new Date(), + rules : [ + "PUT rib", + "GET rib", + "POST rib", + "DELETE rib", + ] }, ]; const collaboratorRules = rules.filter((rule) => rule.namespace === "collaborator"); const notaryRules = [...collaboratorRules, ...rules.filter((rule) => rule.namespace === "notary")]; - const adminRules = [...notaryRules, ...rules.filter((rule) => rule.namespace === "admin")]; + const globalRules = [...notaryRules, ...rules.filter((rule) => rule.namespace === "global")]; + const adminRules = [...globalRules, ...rules.filter((rule) => rule.namespace === "admin")]; const superAdminRules = [...adminRules, ...rules.filter((rule) => rule.namespace === "super-admin")]; const roles: Role[] = [ @@ -839,14 +941,14 @@ export default async function main() { created_at: new Date(), updated_at: new Date(), office: offices[0]!, - rules: notaryRules, + rules: globalRules, }, { name: "Collaborateur", created_at: new Date(), updated_at: new Date(), office: offices[0]!, - rules: notaryRules, + rules: globalRules, }, ]; @@ -1869,6 +1971,20 @@ export default async function main() { role.uid = roleCreated.uid; } + for (const ruleGroup of rulesGroups) { + await prisma.rulesGroups.create({ + data: { + uid: ruleGroup.uid, + name: ruleGroup.name, + rules: { + connect: ruleGroup.rules?.map((rule) => ({ + uid: rules.find((r) => r.name === rule)!.uid!, + })), + }, + }, + }); + } + for (const officeRole of officeRoles) { const officeRoleCreated = await prisma.officeRoles.create({ data: { diff --git a/src/common/databases/seeders/seederOld.ts b/src/common/databases/seeders/seederOld.ts new file mode 100644 index 00000000..160fe314 --- /dev/null +++ b/src/common/databases/seeders/seederOld.ts @@ -0,0 +1,2194 @@ +import { ECivility, ECustomerStatus, EFolderStatus, EOfficeStatus, Prisma, PrismaClient } from "@prisma/client"; +import User, { + Address, + Contact, + Customer, + Deed, + DeedType, + DocumentType, + Office, + OfficeFolder, + OfficeRole, + Role, + Rule, +} from "le-coffre-resources/dist/SuperAdmin"; + +import "module-alias/register"; + +export default async function main() { + try { + const prisma = new PrismaClient(); + + const idNot3 = "rleenrenlnr"; + const idNot4 = "ljfeflecnmd"; + const idNot5 = "rflrefrjf"; + const idNot6 = "er3ojfdlfnd"; + + const addresses: Address[] = [ + { + address: "template", + city: "template", + zip_code: 0o00, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Emillion", + city: "Pacé", + zip_code: 35740, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Charles", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Pologne", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Marcel", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Jacques", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Pierre Pascal", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Maxime Henry", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Maxime Francis", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Avenue Paul Roger", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Avenue Paul Franck", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Avenue Paul Maréchal", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Avenue Marcel Denis", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Place Alexandre", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Place Alexandre Jacques", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Place Alexandre 2", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue du livre", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Place de la joie", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Paul Henry", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + { + address: "Rue Marcelin", + city: "Rennes", + zip_code: 35000, + created_at: new Date(), + updated_at: new Date(), + }, + ]; + + const contacts: Contact[] = [ + { + address: addresses[0], + first_name: "Angela", + last_name: "Dubois", + email: "angela.dubois@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[1], + first_name: "Maxime", + last_name: "Lalo", + email: "maxime.lalo@smart-chain.fr", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[2], + first_name: "Vincent", + last_name: "Alamelle", + email: "vincent.alamelle@smart-chain.fr", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[3], + first_name: "Melissa", + last_name: "Desde", + email: "melissa.desde@smart-chain.fr", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[4], + first_name: "Maxime", + last_name: "Leroy", + email: "maxime.leroy@hotmail.fr", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[5], + first_name: "Paul", + last_name: "Dupont", + email: "paul.dupont@outlook.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[6], + first_name: "Jean", + last_name: "Dubignot", + email: "jean.dubignot@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[7], + first_name: "Vincent", + last_name: "Martin", + email: "vincent.martin@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[8], + first_name: "Lucie", + last_name: "Chevalier", + email: "lucie.chevalier@outlook.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[9], + first_name: "Sébastien", + last_name: "Dubois", + email: "sebastien.dubois@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[10], + first_name: "Mathilde", + last_name: "Durand", + email: "mathilde.durand@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[11], + first_name: "Antoine", + last_name: "Bernard", + email: "antoine.bernard@outlook.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[12], + first_name: "Camille", + last_name: "Laurent", + email: "camille.laurent@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[13], + first_name: "Julien", + last_name: "Mercier", + email: "julien.mercier@hotmail.fr", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[14], + first_name: "Charlotte", + last_name: "Lefebvre", + email: "charlotte.lefebvre@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[15], + first_name: "Caroline", + last_name: "Pallut", + email: "caroline.pallut@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[16], + first_name: "Nadège", + last_name: "Gauchet", + email: "nedege.gauchet@outlook.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[17], + first_name: "Matthieu", + last_name: "Bougeard", + email: "matthieu.bougeard@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + { + address: addresses[18], + first_name: "Cécile", + last_name: "Celton", + email: "cecile.celton@outlook.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.FEMALE, + }, + { + address: addresses[19], + first_name: "Gwendal", + last_name: "Texier", + email: "gwendal.texier@gmail.com", + phone_number: "+33785186013", + cell_phone_number: "+33785186013", + birthdate: null, + created_at: new Date(), + updated_at: new Date(), + civility: ECivility.MALE, + }, + ]; + + const offices: Office[] = [ + { + idNot: "0000", + name: "Office Template", + crpcen: "0000", + address: addresses[0], + created_at: new Date(), + updated_at: new Date(), + office_status: EOfficeStatus.ACTIVATED, + }, + ]; + + const customers: Customer[] = [ + { + contact: contacts[0], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[5], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[6], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[7], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[8], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[9], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[10], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[11], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[12], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[13], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[14], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[15], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[16], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[17], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[18], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + { + contact: contacts[19], + created_at: new Date(), + updated_at: new Date(), + status: ECustomerStatus.PENDING, + }, + ]; + + const rules: Rule[] = [ + { + name: "GET users", + label: "Lecture des utilisateurs", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET offices", + label: "Afficher des offices", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET customers", + label: "Afficher des clients", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET deeds", + label: "Voir des types d'acte", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET deed-types", + label: "Lecture des types d'actes", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET documents", + label: "Afficher des documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET document-types", + label: "Lecture des types de documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET files", + label: "Lecture des fichiers", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET folders", + label: "Afficher les dossiers", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET roles", + label: "Afficher les rôles", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET rules", + label: "Afficher les droits", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "GET office-roles", + label: "Lecture des rôles d'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "collaborator", + }, + { + name: "POST deeds", + label: "Créer un template de type d'acte", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "PUT deeds", + label: "Modifier un type d'acte", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "DELETE deeds", + label: "Supprimer des types d'actes", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST folders", + label: "Créer un dossier", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "PUT folders", + label: "Modifier des dossiers", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "DELETE folders", + label: "Supprimer un dossier vide", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST documents", + label: "Demander des documents à un client", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "PUT documents", + label: "Valider des documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "DELETE documents", + label: "Supprimer un document demandé", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST customers", + label: "Créer des clients", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "PUT customers", + label: "Modifier des clients", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "DELETE customers", + label: "Supprimer un client", + created_at: new Date(), + updated_at: new Date(), + namespace: "super-admin", + }, + { + name: "POST anchors", + label: "Ancrer un dossier", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "GET anchors", + label: "Vérifier l'ancrage un dossier", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST deed-types", + label: "Création des types d'actes", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "PUT deed-types", + label: "Modification des types d'actes", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "DELETE deed-types", + label: "Suppression des types d'actes", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "POST document-types", + label: "Création des types de documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "PUT document-types", + label: "Modification des types de documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "DELETE document-types", + label: "Suppression des types de documents", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "POST office-roles", + label: "Création des rôles d'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "PUT office-roles", + label: "Modification des rôles d'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "DELETE office-roles", + label: "Suppression des rôles d'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "PUT users", + label: "Modification des utilisateurs", + created_at: new Date(), + updated_at: new Date(), + namespace: "admin", + }, + { + name: "DELETE office-roles", + label: "Suppression des rôles d'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "super-admin", + }, + { + name: "PUT offices", + label: "Modification des offices", + created_at: new Date(), + updated_at: new Date(), + namespace: "super-admin", + }, + { + name: "PUT rib", + label: "Editer le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "GET rib", + label: "Lire le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST rib", + label: "Déposer le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "DELETE rib", + label: "Supprimer le RIB de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "GET subscriptions", + label: "Récupérer les abonnements", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST subscriptions", + label: "Inviter un collaborateur à l'abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "PUT subscriptions", + label: "Modifier l'abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "GET stripe", + label: "Gérer l'abonnement de l'office", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + { + name: "POST stripe", + label: "Payer un abonnement", + created_at: new Date(), + updated_at: new Date(), + namespace: "notary", + }, + ]; + + const rulesGroups = [ + { + name: "Gestion des matrices d'actes et des documents", + created_at: new Date(), + updated_at: new Date(), + rules: [ + "POST deeds", + "PUT deeds", + "DELETE deeds", + "GET document-types", + "DELETE deed-types", + "PUT deed-types", + "DELETE document-types", + "GET deed-types", + "POST document-types", + "POST deed-types", + "PUT document-types", + ] + }, + { + name: "Gestion de l'abonnement", + created_at: new Date(), + updated_at: new Date(), + rules : [ + "GET subscriptions", + "POST subscriptions", + "PUT subscriptions", + "GET stripe", + "POST stripe", + ] + }, + { + name: "Intégration du RIB", + created_at: new Date(), + updated_at: new Date(), + rules : [ + "PUT rib", + "GET rib", + "POST rib", + "DELETE rib", + ] + }, + ]; + + const collaboratorRules = rules.filter((rule) => rule.namespace === "collaborator"); + const notaryRules = [...collaboratorRules, ...rules.filter((rule) => rule.namespace === "notary")]; + const adminRules = [...notaryRules, ...rules.filter((rule) => rule.namespace === "admin")]; + const superAdminRules = [...adminRules, ...rules.filter((rule) => rule.namespace === "super-admin")]; + + const roles: Role[] = [ + { + name: "super-admin", + label: "Super administrateur", + created_at: new Date(), + updated_at: new Date(), + rules: superAdminRules, + }, + { + name: "admin", + label: "Administrateur", + created_at: new Date(), + updated_at: new Date(), + rules: adminRules, + }, + { + name: "notary", + label: "Notaire", + created_at: new Date(), + updated_at: new Date(), + rules: [], + }, + { + name: "default", + label: "Utilisateur", + created_at: new Date(), + updated_at: new Date(), + rules: [], + }, + ]; + + const officeRoles: OfficeRole[] = [ + { + name: "Notaire", + created_at: new Date(), + updated_at: new Date(), + office: offices[0]!, + rules: notaryRules, + }, + { + name: "Collaborateur", + created_at: new Date(), + updated_at: new Date(), + office: offices[0]!, + rules: notaryRules, + }, + ]; + + const users: User[] = [ + { + created_at: new Date(), + updated_at: new Date(), + idNot: idNot3, + contact: contacts[1], + office_membership: offices[0], + role: roles[1], + }, + { + created_at: new Date(), + updated_at: new Date(), + idNot: idNot4, + contact: contacts[2], + office_membership: offices[0], + role: roles[2], + office_role: officeRoles[2], + }, + { + created_at: new Date(), + updated_at: new Date(), + idNot: idNot5, + contact: contacts[3], + office_membership: offices[0], + role: roles[1], + office_role: officeRoles[3], + }, + { + created_at: new Date(), + updated_at: new Date(), + idNot: idNot6, + contact: contacts[4], + office_membership: offices[0], + role: roles[3], + }, + ]; + + const documentTypes: DocumentType[] = [ + { + archived_at: null, + name: "Carte Nationale d'Identité recto-verso", + office: offices[0], + private_description: "Document d'identité", + public_description: "Document d'identité", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Contrat de mariage", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Convention de PACS", + office: offices[0], + private_description: " ", + public_description: "Avec précision du régime", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Titre de propriété", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Cahier des charges du lotissement", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Règlement du lotissement", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Cahier de prescriptions architecturales", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Statuts de l'association syndicale libre", + office: offices[0], + private_description: " ", + public_description: "Avec nom et adresse du président", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Etat descriptif de division et règlement de copropriété", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Etat descriptif de division et règlement de copropriété modificatifs", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Carnet d'entretien", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Trois derniers PV D'AG", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Trois derniers relevés de charge", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Bail ou congé délivré", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Certificat d'assainissement", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Facture ou contrat d'electricité", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Facture ou contrat de gaz", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Loi carrez", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Etat parasitaire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Amiante", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Facture des travaux", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Garanties décénales des entreprises", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "PV de réception des travaux", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Permis de construire et modificatifs", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Déclaration attestation l'achèvement des travaux", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Attestation de non opposition", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Assurance décenale", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Dommage ouvrage", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Dernière facture ramonage", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Liste du mobilier", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "RIB daté et signé", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Extrait KBIS et statuts de la société", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Document arpentage et plan de division", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Accord de prêt", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Offre de prêt", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Attestation dépôt de permis de construire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Permis de construire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Extrait acte de décès", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Fiche de renseignements d'états civils", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Donation entre epoux", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Testament", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Liste des banques avce un compte bancaire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Contrat d'assurance-vie", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Liste des caisses versant une retraite ou une pension", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Dernière déclaration de revenus", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Avis d'imposition (revenus et ISF)", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Avis d'imposition (foncier et habitation)", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Liste des emprunts en cours", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Actes de donation", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Déclarations de dons faites aux impôts", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Actes de succession", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Actes de vente de biens", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Factures de travaux", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Bail de location", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Nom et adresse du Syndic", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Liste des autres biens", + office: offices[0], + private_description: " ", + public_description: + "Exemple: PEE, PER, parts de société, bijoux de valeur et oeuvres d'art, fonds de commerce, fonds artisanal, entreprise commerciale, exploitation agricole...", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Caution, Prestation compensatoire, Pension alimentaire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Titre de propriété des biens", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Tableaux d'amortissement des prêts immobiliers", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Cartes grises et cotes ARGUS des véhicules", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Etat des avoirs bancaires", + office: offices[0], + private_description: " ", + public_description: "Joints ou individuels au jour de la séparation", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Avoirs mobiliers", + office: offices[0], + private_description: " ", + public_description: + "arrêtés à la date de la séparation (relevés de comptes épargne entreprise, épargne retraite, assurance-vie...)", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Livret de famille", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Dernières régularisations de charges", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Diagnostic de Performance Energétique (DPE)", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Avis d'imposition N-1", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Avis d'imposition N-2", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Contrat de travail", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Trois derniers bulletins de salaire", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Bilan comptable", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Trois dernières quittances de loyers ou attestation d'hébergement", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Taxe foncière", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + archived_at: null, + name: "Justificatif de domicile", + office: offices[0], + private_description: " ", + public_description: " ", + created_at: new Date(), + updated_at: new Date(), + }, + { + name: "Autres documents", + archived_at: null, + public_description: "Autres documents", + private_description: "Autres documents", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + }, + ]; + + const deedTypes: DeedType[] = [ + { + name: "Promesse de vente", + archived_at: null, + description: "Promesse de vente entre deux partis", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: [...documentTypes.slice(0, 31), documentTypes[66]!], + }, + { + name: "Acte de vente", + archived_at: null, + description: "Acte de vente", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: documentTypes.slice(30, 36), + }, + { + name: "Acte de succession", + archived_at: null, + description: "Acte de succession", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: [...documentTypes.slice(36, 56), documentTypes[62]!, documentTypes[30]!], + }, + { + name: "Acte de divorce", + archived_at: null, + description: "Acte de divorce", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: [...documentTypes.slice(57, 62), documentTypes[30]!, documentTypes[0]!], + }, + { + name: "Acte de donation", + archived_at: null, + description: "Acte de donation", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: [documentTypes[0]!, documentTypes[62]!, documentTypes[57]!], + }, + { + name: "Bail d'habitation", + archived_at: null, + description: "Bail d'habitation", + office: offices[0], + created_at: new Date(), + updated_at: new Date(), + document_types: [documentTypes[0]!, documentTypes[30]!, documentTypes[57]!, ...documentTypes.slice(62)], + }, + ]; + + const deeds: Deed[] = [ + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[0], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[1], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + { + deed_type: deedTypes[2], + created_at: new Date(), + updated_at: new Date(), + }, + ]; + + const officeFolders: OfficeFolder[] = [ + { + folder_number: "0001", + name: "Vente par Mme. Simon au profit de Mme. Lextrait", + deed: deeds[0], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0002", + name: "Donation des parts sociales de la société SARL FLORE", + deed: deeds[1], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0003", + name: "Vente par Mme. Lefebvre au profit de Mme. Mathieu", + deed: deeds[2], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0004", + name: "Vente par Mme. Chevalier au profit de M. Daveau", + deed: deeds[3], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0005", + name: "Vente par M. Lalo au profit de Mme. Pigale", + deed: deeds[4], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0006", + name: "Donation des parts sociales de la société SMART-TALENT", + deed: deeds[5], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0007", + name: "Vente par M. Girard au profit de M. Louis", + deed: deeds[6], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0008", + name: "Vente par Mme. Leclerc au profit de M. Louis", + deed: deeds[7], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + { + folder_number: "0009", + name: "Vente par M. Lambert au profit de M. Holmes", + deed: deeds[8], + status: EFolderStatus.LIVE, + created_at: new Date(), + updated_at: new Date(), + office: offices[0], + description: null, + archived_description: null, + }, + ]; + + for (const office of offices) { + const officeCreated = await prisma.offices.create({ + data: { + idNot: office.idNot!, + name: office.name, + crpcen: office.crpcen, + address: { + create: { + address: office.address!.address, + zip_code: office.address!.zip_code, + city: office.address!.city, + }, + }, + office_status: EOfficeStatus.ACTIVATED, + }, + }); + office.uid = officeCreated.uid; + } + for (const rule of rules) { + const ruleCreated = await prisma.rules.create({ + data: { + name: rule.name, + label: rule.label, + namespace: rule.namespace, + }, + }); + rule.uid = ruleCreated.uid; + } + + for (const role of roles) { + const roleCreated = await prisma.roles.create({ + data: { + name: role.name, + label: role.label, + rules: { + connect: role.rules?.map((rule) => ({ + uid: rule.uid!, + })), + }, + }, + }); + role.uid = roleCreated.uid; + } + + for (const ruleGroup of rulesGroups) { + await prisma.rulesGroups.create({ + data: { + name: ruleGroup.name, + rules: { + connect: ruleGroup.rules?.map((rule) => ({ + uid: rules.find((r) => r.name === rule)!.uid!, + })), + }, + }, + }); + } + + for (const officeRole of officeRoles) { + const officeRoleCreated = await prisma.officeRoles.create({ + data: { + name: officeRole.name, + office: { + connect: { + uid: officeRole.office.uid, + }, + }, + rules: { + connect: officeRole.rules?.map((rule) => ({ + uid: rule.uid!, + })), + }, + }, + }); + officeRole.uid = officeRoleCreated.uid; + } + + for (const user of users) { + const createArgs: Prisma.UsersCreateArgs = { + data: { + idNot: user.idNot, + office_membership: { + connectOrCreate: { + where: { + idNot: user.office_membership!.idNot, + }, + create: { + idNot: user.office_membership!.idNot!, + name: user.office_membership!.name, + crpcen: user.office_membership!.crpcen, + address: { + create: { + address: user.office_membership!.address!.address, + zip_code: user.office_membership!.address!.zip_code, + city: user.office_membership!.address!.city, + }, + }, + }, + }, + }, + contact: { + create: { + first_name: user.contact!.first_name, + last_name: user.contact!.last_name, + email: user.contact!.email, + phone_number: user.contact!.phone_number, + cell_phone_number: user.contact!.cell_phone_number, + civility: ECivility[user.contact!.civility as keyof typeof ECivility], + address: {}, + }, + }, + role: { + connect: { + uid: user.role!.uid, + }, + }, + }, + }; + if (user.contact!.address) { + createArgs.data.contact!.create!.address!.create = { + address: user.contact!.address.address, + zip_code: user.contact!.address.zip_code, + city: user.contact!.address.city, + }; + } + if (user.office_role) { + createArgs.data.office_role = { + connect: { + uid: user.office_role.uid, + }, + }; + } + const userCreated = await prisma.users.create(createArgs); + user.uid = userCreated.uid; + } + + for (const customer of customers) { + const createArgs: Prisma.CustomersCreateArgs = { + data: { + status: ECustomerStatus.PENDING, + contact: { + create: { + first_name: customer.contact!.first_name, + last_name: customer.contact!.last_name, + email: customer.contact!.email, + phone_number: customer.contact!.phone_number, + cell_phone_number: customer.contact!.cell_phone_number, + civility: ECivility[customer.contact!.civility as keyof typeof ECivility], + address: {}, + }, + }, + }, + }; + + if (customer.contact?.address) { + createArgs.data.contact!.create!.address = { + create: { + address: customer.contact?.address?.address, + zip_code: customer.contact?.address?.zip_code, + city: customer.contact?.address?.city, + }, + }; + } + const customersCreated = await prisma.customers.create(createArgs); + customer.uid = customersCreated.uid; + } + + for (const documentType of documentTypes) { + const documentTypeCreated = await prisma.documentTypes.create({ + data: { + name: documentType.name, + public_description: documentType.public_description || "", + private_description: documentType.private_description, + office: { + connect: { + uid: documentType.office!.uid, + }, + }, + }, + }); + documentType.uid = documentTypeCreated.uid; + } + + for (const deedType of deedTypes) { + const createArgs: Prisma.DeedTypesCreateArgs = { + data: { + name: deedType.name, + description: deedType.description || "", + office: { + connect: { + uid: deedType.office!.uid, + }, + }, + }, + }; + if (deedType.document_types) { + createArgs.data.document_types = { + connect: deedType.document_types.map((documentType) => ({ + uid: documentType.uid, + })), + }; + } + const deedTypeCreated = await prisma.deedTypes.create(createArgs); + deedType.uid = deedTypeCreated.uid; + } + + for (const deed of deeds) { + const createArgs: Prisma.DeedsCreateArgs = { + data: { + deed_type: { + connect: { + uid: deed.deed_type!.uid, + }, + }, + }, + }; + const deedTypeWithDocumentTypes = await prisma.deedTypes.findUniqueOrThrow({ + where: { + uid: deed.deed_type!.uid, + }, + include: { document_types: true }, + }); + + if (deedTypeWithDocumentTypes.document_types) { + createArgs.data.document_types = { + connect: deedTypeWithDocumentTypes.document_types.map((documentType) => ({ + uid: documentType.uid, + })), + }; + } + const deedCreated = await prisma.deeds.create(createArgs); + deed.uid = deedCreated.uid; + } + + for (const officeFolder of officeFolders) { + const officeFolderCreated = await prisma.officeFolders.create({ + data: { + folder_number: officeFolder.folder_number, + name: officeFolder.name, + description: officeFolder.description, + status: EFolderStatus.LIVE, + deed: { + connect: { + uid: officeFolder.deed?.uid, + }, + }, + office: { + connect: { + uid: officeFolder.office!.uid, + }, + }, + stakeholders: { + connect: officeFolder.stakeholders?.map((stakeholder) => ({ + uid: stakeholder.uid!, + })), + }, + }, + }); + officeFolder.uid = officeFolderCreated.uid; + } + + console.log(">MOCK DATA - Seeding completed!"); + } catch (error) { + console.log(error); + console.log("Data already seeded, skiping"); + } +} +main(); diff --git a/src/common/emails/EmailBuilder.ts b/src/common/emails/EmailBuilder.ts index 81eb1c1e..5dcfb7b8 100644 --- a/src/common/emails/EmailBuilder.ts +++ b/src/common/emails/EmailBuilder.ts @@ -106,4 +106,35 @@ export default class EmailBuilder { if (civility === "MALE") return "Mr"; else return "Mme"; } + + public async sendInvitationEmails(emails: string[]) { + emails.forEach((email) => { + const to = email; + + const templateVariables = { + link: this.variables.APP_HOST, + idNotLink: this.variables.IDNOT_PROD_BASE_URL, + }; + + const templateName = ETemplates.SUBSCRIPTION_INVITATION; + const subject = "Invitation abonnement LeCoffre"; + + this.mailchimpService.create({ + templateName, + to, + subject, + templateVariables, + uid: "", + from: null, + cc: [], + cci: [], + sentAt: null, + nbTrySend: null, + lastTrySendDate: null, + }); + // this.mailchimpService.sendEmails(); + }); + + + } } diff --git a/src/common/emails/Templates/EmailTemplates.ts b/src/common/emails/Templates/EmailTemplates.ts index 081cfadb..520098e2 100644 --- a/src/common/emails/Templates/EmailTemplates.ts +++ b/src/common/emails/Templates/EmailTemplates.ts @@ -2,4 +2,5 @@ export const ETemplates = { DOCUMENT_ASKED: "DOCUMENT_ASKED", DOCUMENT_REFUSED: "DOCUMENT_REFUSED", DOCUMENT_RECAP: "DOCUMENT_RECAP", + SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION", }; \ No newline at end of file diff --git a/src/common/repositories/RulesGroupsRepository.ts b/src/common/repositories/RulesGroupsRepository.ts new file mode 100644 index 00000000..6c263cb1 --- /dev/null +++ b/src/common/repositories/RulesGroupsRepository.ts @@ -0,0 +1,27 @@ +import Database from "@Common/databases/database"; +import BaseRepository from "@Repositories/BaseRepository"; +import { Service } from "typedi"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class RulesGroupsRepository extends BaseRepository { + constructor(private database: Database) { + super(); + } + protected get model() { + return this.database.getClient().rulesGroups; + } + protected get instanceDb() { + return this.database.getClient(); + } + + /** + * @description : Find many subscriptions + */ + public async findMany(query: Prisma.RulesGroupsFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); + if (!query.include) return this.model.findMany({ ...query }); + return this.model.findMany({ ...query }); + } + +} diff --git a/src/common/repositories/SeatsRepository.ts b/src/common/repositories/SeatsRepository.ts new file mode 100644 index 00000000..4a3a255d --- /dev/null +++ b/src/common/repositories/SeatsRepository.ts @@ -0,0 +1,78 @@ +import Database from "@Common/databases/database"; +import BaseRepository from "@Repositories/BaseRepository"; +import { Service } from "typedi"; +import { Prisma, Seats } from "@prisma/client"; + +@Service() +export default class SeatsRepository extends BaseRepository { + constructor(private database: Database) { + super(); + } + protected get model() { + return this.database.getClient().seats; + } + protected get instanceDb() { + return this.database.getClient(); + } + + /** + * @description : Find many seats + */ + public async findMany(query: Prisma.SeatsFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); + if (!query.include) return this.model.findMany({ ...query }); + return this.model.findMany({ ...query }); + } + + /** + * @description : find unique seat + */ + public async findOneByUid(uid: string, query?: Prisma.SeatsInclude): Promise { + return this.model.findUnique({ + where: { + uid: uid, + }, + include: query, + }); + } + + /** + * @description : Create a seat + */ + public async create(userUid: string, subscriptionUid: string): Promise { + + const createArgs: Prisma.SeatsCreateArgs = { + data: { + subscription: { + connect: { + uid: subscriptionUid + }, + }, + user: { + connect: { + uid: userUid + }, + }, + }, + }; + { + + return this.model.create(createArgs); + } + + + } + + /** + * @description : Delete a seat + */ + public async delete(uid: string) { + return this.model.delete({ + where: { + uid: uid, + }, + }); + } + + +} diff --git a/src/common/repositories/SubscriptionsRepository.ts b/src/common/repositories/SubscriptionsRepository.ts new file mode 100644 index 00000000..4394495b --- /dev/null +++ b/src/common/repositories/SubscriptionsRepository.ts @@ -0,0 +1,154 @@ +import Database from "@Common/databases/database"; +import BaseRepository from "@Repositories/BaseRepository"; +import { Service } from "typedi"; +import { ESubscriptionStatus, ESubscriptionType, Prisma, Subscriptions } from "@prisma/client"; +import { Subscription } from "le-coffre-resources/dist/Admin"; + +@Service() +export default class SubscriptionsRepository extends BaseRepository { + constructor(private database: Database) { + super(); + } + protected get model() { + return this.database.getClient().subscriptions; + } + protected get instanceDb() { + return this.database.getClient(); + } + + /** + * @description : Find many subscriptions + */ + public async findMany(query: Prisma.SubscriptionsFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); + if (!query.include) return this.model.findMany({ ...query }); + return this.model.findMany({ ...query }); + } + + /** + * @description : find unique subscription + */ + public async findOneByUid(uid: string, query?: Prisma.SubscriptionsInclude): Promise { + return this.model.findUnique({ + where: { + uid: uid, + }, + include: query, + }); + } + + /** + * @description : Create a subscription + */ + public async create(subscription: Subscription): Promise { + if(subscription.type === "STANDARD") + { + const createArgs: Prisma.SubscriptionsCreateArgs = { + data: { + start_date: subscription.start_date, + end_date: subscription.end_date, + type: ESubscriptionType.STANDARD, + status: ESubscriptionStatus.ACTIVE, + nb_seats: subscription.nb_seats!, + stripe_subscription_id: subscription.stripe_subscription_id || "", + office: { + connect: { + uid: subscription.office!.uid, + }, + }, + }, + }; + return this.model.create(createArgs); + } + else + { + const createArgs: Prisma.SubscriptionsCreateArgs = { + data: { + start_date: subscription.start_date, + end_date: subscription.end_date, + type: ESubscriptionType.UNLIMITED, + status: ESubscriptionStatus.ACTIVE, + nb_seats: 0, + stripe_subscription_id: subscription.stripe_subscription_id || "", + office: { + connect: { + uid: subscription.office!.uid, + }, + }, + }, + }; + return this.model.create(createArgs); + } + + } + + /** + * @description : update given subscription + */ + public async update(uid: string, subscription: Subscription): Promise { + 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 = { + where: { + uid: uid, + }, + data: { + end_date: subscription.end_date, + type: ESubscriptionType.STANDARD, + status: subscription.status as ESubscriptionStatus, + nb_seats: subscription.nb_seats!, + }, + }; + return this.model.update(updateArgs); + } + else + { + const updateArgs: Prisma.SubscriptionsUpdateArgs = { + where: { + uid: uid, + }, + data: { + end_date: subscription.end_date, + type: ESubscriptionType.UNLIMITED, + status: subscription.status as ESubscriptionStatus, + nb_seats: 0, + seats: { + deleteMany: {}, + }, + }, + }; + return this.model.update(updateArgs); + } + } + + /** + * @description : Delete a subscription + */ + public async delete(uid: string) { + return this.model.delete({ + where: { + uid: uid, + }, + }); + } + + +} diff --git a/src/common/repositories/UserWhitelistRepository.ts b/src/common/repositories/UserWhitelistRepository.ts new file mode 100644 index 00000000..eae35819 --- /dev/null +++ b/src/common/repositories/UserWhitelistRepository.ts @@ -0,0 +1,39 @@ +import Database from "@Common/databases/database"; +import BaseRepository from "@Repositories/BaseRepository"; +import { Service } from "typedi"; +import { Prisma } from "prisma/prisma-client"; + +@Service() +export default class UserWhitelistRepository extends BaseRepository { + constructor(private database: Database) { + super(); + } + protected get model() { + return this.database.getClient().userWhitelist; + } + protected get instanceDb() { + return this.database.getClient(); + } + + /** + * @description : Find many whitelist + */ + public async findMany(query: Prisma.UserWhitelistFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); + return this.model.findMany(query); + } + + /** + * @description : find unique by email + */ + public async findOneByIdNotId(idNotId: string) { + return this.model.findMany({ + where: { + idNot: idNotId, + }, + }); + } + + + +} diff --git a/src/common/webhooks/stripeWebhooks.ts b/src/common/webhooks/stripeWebhooks.ts new file mode 100644 index 00000000..5885b85c --- /dev/null +++ b/src/common/webhooks/stripeWebhooks.ts @@ -0,0 +1,104 @@ +import { BackendVariables } from "@Common/config/variables/Variables"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Controller, Post } from "@ControllerPattern/index"; +import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts"; +import StripeService from "@Services/common/StripeService/StripeService"; +import { validateOrReject } from "class-validator"; +import { Request, Response } from "express"; +import { Subscription } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Controller() +@Service() +export default class StripeWebhooks extends ApiController { + constructor(private stripeService: StripeService, private subscriptionsService: SubscriptionsService, private backendVariables: BackendVariables) { + super(); + } + + /** + * @description Create a new checkout session + */ + @Post("/api/v1/webhooks/stripe") + protected async post(req: Request, response: Response) { + try { + const event = req.body; + + switch (event.type) { + //Manage plan switch + recurring payment + case "invoice.payment_succeeded": + if (event.data.object.billing_reason !== "subscription_update" && event.data.object.billing_reason !== "subscription_cycle") break; + const stripeSubscription = await this.stripeService.getClient().subscriptions.retrieve(event.data.object.subscription); + if(stripeSubscription.metadata['env'] !== this.backendVariables.ENV) break; + + const existingSubscription = await this.subscriptionsService.get({where : {stripe_subscription_id : stripeSubscription.id}}); + + if(!existingSubscription[0]) break; + + const subscriptionUpdate: any = {}; + subscriptionUpdate.start_date = new Date(stripeSubscription.current_period_start * 1000); + subscriptionUpdate.end_date = new Date(stripeSubscription.current_period_end * 1000); + subscriptionUpdate.nb_seats = stripeSubscription.items.data[0]?.quantity; + + if(stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID || stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID){ + subscriptionUpdate.type = "STANDARD"; + } + else{ + subscriptionUpdate.type = "UNLIMITED"; + } + + const subscriptionEntityUpdate = Subscription.hydrate(subscriptionUpdate); + + await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false }); + + await this.subscriptionsService.update(existingSubscription[0].uid ,subscriptionEntityUpdate); + + //Manage subscription creation and first payment + case "checkout.session.completed": + if (event.data.object.status !== "complete") break; + + const subscription = JSON.parse(event.data.object.metadata.subscription); + const env = event.data.object.metadata.env; + + if (env !== this.backendVariables.ENV) break; + + subscription.stripe_subscription_id = event.data.object.subscription; + + await this.stripeService.getClient().subscriptions.update(subscription.stripe_subscription_id, { + metadata: { + env: env, + }, + } + ) + + const subscriptionInfo = await this.stripeService + .getClient() + .subscriptions.retrieve(subscription.stripe_subscription_id); + + subscription.start_date = new Date(subscriptionInfo.current_period_start * 1000); + subscription.end_date = new Date(subscriptionInfo.current_period_end * 1000); + + const subscriptionEntity = Subscription.hydrate(subscription); + + await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); + + await this.subscriptionsService.create(subscriptionEntity); + break; + + //Manage plan expiration + case "customer.subscription.deleted": + const currentSubscription = await this.stripeService.getClient().subscriptions.retrieve(event.data.object.id); + if(currentSubscription.metadata['env'] !== this.backendVariables.ENV) break; + const subscriptionToDelete = await this.subscriptionsService.get({where : {stripe_subscription_id : event.data.object.id}}); + if(!subscriptionToDelete[0]) break; + await this.subscriptionsService.delete(subscriptionToDelete[0].uid); + default: + break; + } + + response.json({ received: true }); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/entries/App.ts b/src/entries/App.ts index 5dd1bd04..d94c9766 100644 --- a/src/entries/App.ts +++ b/src/entries/App.ts @@ -14,7 +14,7 @@ const storage = multer.memoryStorage(); (async () => { try { - const variables = await Container.get(BackendVariables).validate(); + const variables = await Container.get(BackendVariables).validate(); const port = variables.APP_PORT; const rootUrl = variables.APP_ROOT_URL; const label = variables.APP_LABEL ?? "Unknown Service"; diff --git a/src/services/admin/RulesGroupsService/RulesGroupsService.ts b/src/services/admin/RulesGroupsService/RulesGroupsService.ts new file mode 100644 index 00000000..64dd738c --- /dev/null +++ b/src/services/admin/RulesGroupsService/RulesGroupsService.ts @@ -0,0 +1,20 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import { Prisma } from "@prisma/client"; +import RulesGroupsRepository from "@Repositories/RulesGroupsRepository"; + +@Service() +export default class RulesGroupsService extends BaseService { + constructor(private rulesGroupsRepository: RulesGroupsRepository) { + super(); + } + + /** + * @description : Get all subscriptions + * @throws {Error} If subscriptions cannot be get + */ + public get(query: Prisma.RulesGroupsFindManyArgs) { + return this.rulesGroupsRepository.findMany(query); + } +} diff --git a/src/services/admin/SeatsService/SeatsService.ts b/src/services/admin/SeatsService/SeatsService.ts new file mode 100644 index 00000000..f4f2dcbb --- /dev/null +++ b/src/services/admin/SeatsService/SeatsService.ts @@ -0,0 +1,45 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import { Prisma, Seats } from "@prisma/client"; +import SeatsRepository from "@Repositories/SeatsRepository"; + +@Service() +export default class SeatsService extends BaseService { + constructor(private seatsRepository: SeatsRepository) { + super(); + } + + /** + * @description : Get all seats + * @throws {Error} If seats cannot be get + */ + public get(query: Prisma.SeatsFindManyArgs) { + return this.seatsRepository.findMany(query); + } + + /** + * @description : Get a seat by uid + * @throws {Error} If seat is not found + */ + public async getByUid(uid: string, query?: Prisma.SeatsInclude) { + return this.seatsRepository.findOneByUid(uid, query); + } + + /** + * @description : Create a new seat + * @throws {Error} If seat cannot be created + */ + public async create(subscriptionUid: string, userUid: string): Promise { + 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); + } + +} diff --git a/src/services/admin/SubscriptionsService/SubscriptionsService.ts.ts b/src/services/admin/SubscriptionsService/SubscriptionsService.ts.ts new file mode 100644 index 00000000..77120e70 --- /dev/null +++ b/src/services/admin/SubscriptionsService/SubscriptionsService.ts.ts @@ -0,0 +1,63 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +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, private seatsService: SeatsService) { + super(); + } + + /** + * @description : Get all subscriptions + * @throws {Error} If subscriptions cannot be get + */ + public get(query: Prisma.SubscriptionsFindManyArgs) { + return this.subscriptionsRepository.findMany(query); + } + + /** + * @description : Get a subscription by uid + * @throws {Error} If subscription is not found + */ + public async getByUid(uid: string, query?: Prisma.SubscriptionsInclude) { + return this.subscriptionsRepository.findOneByUid(uid, query); + } + + /** + * @description : Create a new subscription + * @throws {Error} If subsctiption cannot be created + */ + public async create(subscriptionEntity: Subscription): Promise { + return this.subscriptionsRepository.create(subscriptionEntity); + } + + /** + * @description : Modify a subscription + * @throws {Error} If subscription cannot be modified + */ + public async update(uid: string, subscriptionEntity: Subscription): Promise { + 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); + } + + /** + * @description : Delete a subscription + * @throws {Error} If subscription cannot be deleted + */ + public async delete(uid: string) { + return this.subscriptionsRepository.delete(uid); + } +} diff --git a/src/services/common/AuthService/AuthService.ts b/src/services/common/AuthService/AuthService.ts index 6be3ae25..b3fa7591 100644 --- a/src/services/common/AuthService/AuthService.ts +++ b/src/services/common/AuthService/AuthService.ts @@ -8,7 +8,7 @@ import { ECustomerStatus } from "@prisma/client"; import { Customer } from "le-coffre-resources/dist/Notary"; import bcrypt from "bcrypt"; -enum PROVIDER_OPENID { +export enum PROVIDER_OPENID { idNot = "idNot", } diff --git a/src/services/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index 2cfe91ee..7104c4c5 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -122,13 +122,14 @@ export default class IdNotService extends BaseService { grant_type: "authorization_code", }); + const token = await fetch(this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query, { method: "POST" }); if(token.status !== 200) console.error(await token.text()); const decodedToken = (await token.json()) as IIdNotToken; - const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload; + const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload; return decodedIdToken; @@ -147,7 +148,7 @@ export default class IdNotService extends BaseService { case EIdnotRole.SUPPLEANT: return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; case EIdnotRole.ADMINISTRATEUR: - return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; + return (await this.rolesService.get({ where: { name: "admin" } }))[0]!; case EIdnotRole.CURATEUR: return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; default: diff --git a/src/services/common/MailchimpService/MailchimpService.ts b/src/services/common/MailchimpService/MailchimpService.ts index 03df091e..9e0f4d09 100644 --- a/src/services/common/MailchimpService/MailchimpService.ts +++ b/src/services/common/MailchimpService/MailchimpService.ts @@ -52,7 +52,7 @@ export default class MailchimpService extends BaseService { * @throws {Error} If email cannot be sent */ public async sendEmails() { - const emailsToSend = await this.get({ where: { sentAt: null } }); + const emailsToSend = await this.get({ where: { sentAt: null } }); const currentDate = new Date(); let nextTrySendDate = null; diff --git a/src/services/common/StripeService/StripeService.ts b/src/services/common/StripeService/StripeService.ts new file mode 100644 index 00000000..3ff92724 --- /dev/null +++ b/src/services/common/StripeService/StripeService.ts @@ -0,0 +1,59 @@ +import { BackendVariables } from "@Common/config/variables/Variables"; +import { Subscription } from "le-coffre-resources/dist/Admin"; +import Stripe from "stripe"; +import { Service } from "typedi"; + +@Service() +export default class StripeService { + private client: Stripe; + constructor(protected variables: BackendVariables) { + this.client = new Stripe(variables.STRIPE_SECRET_KEY); + } + + public getClient(): Stripe { + return this.client; + } + + public async createCheckoutSession(subscription: Subscription) { + const priceId = subscription.type === "STANDARD" ? this.variables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID : this.variables.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID; + return this.client.checkout.sessions.create({ + mode: "subscription", + payment_method_types: ["card", "paypal"], + billing_address_collection: "auto", + line_items: [ + { + price: priceId, + quantity: subscription.type === "STANDARD" ? subscription.nb_seats : 1, + }, + ], + success_url: this.variables.APP_HOST + "/subscription/success", + cancel_url: this.variables.APP_HOST + "/subscription/error", + metadata: { + subscription: JSON.stringify(subscription), + env: this.variables.ENV, + }, + allow_promotion_codes: true, + }); + + + + } + + public async getStripeSubscriptionByUid(subscriptionId: string) { + return await this.client.subscriptions.retrieve(subscriptionId); + } + + public async createClientPortalSession(subscriptionId: string) { + const subscription = await this.client.subscriptions.retrieve(subscriptionId); + + return this.client.billingPortal.sessions.create({ + customer: subscription.customer as string, + return_url: this.variables.APP_HOST + "/subscription/manage", + }); + } + + public async getCustomerBySubscription(subscriptionId: string) { + const subscription = await this.client.subscriptions.retrieve(subscriptionId); + return this.client.customers.retrieve(subscription.customer as string); + } +} diff --git a/src/services/common/UserWhitelistService/WhitelistService.ts b/src/services/common/UserWhitelistService/WhitelistService.ts new file mode 100644 index 00000000..dd96268f --- /dev/null +++ b/src/services/common/UserWhitelistService/WhitelistService.ts @@ -0,0 +1,14 @@ +import UserWhitelistRepository from "@Repositories/UserWhitelistRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class UserWhitelistService extends BaseService { + constructor(private userWhitelistRepository: UserWhitelistRepository) { + super(); + } + + public async getByIdNotId(idNotId: string): Promise { + return this.userWhitelistRepository.findOneByIdNotId(idNotId); + } +} diff --git a/src/services/super-admin/UsersService/UsersService.ts b/src/services/super-admin/UsersService/UsersService.ts index 3598e945..0781623c 100644 --- a/src/services/super-admin/UsersService/UsersService.ts +++ b/src/services/super-admin/UsersService/UsersService.ts @@ -2,7 +2,7 @@ import BaseService from "@Services/BaseService"; import "reflect-metadata"; import { Service } from "typedi"; import UsersRepository from "@Repositories/UsersRepository"; -import User from "le-coffre-resources/dist/SuperAdmin"; +import User from "le-coffre-resources/dist/Admin"; import { Prisma, Users } from "@prisma/client"; @Service() @@ -91,4 +91,5 @@ export default class UsersService extends BaseService { return this.userRepository.findManyToCheck(); } + }