Download src/modules/subscriptions/subscriptions.repository.js from moustafa7/gymco: direct link, hf CLI and curl.
- Browser
- Download file 5.39 kB
-
https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/modules/subscriptions/subscriptions.repository.js
- Command line
-
hf download hf://spaces/moustafa7/gymco/src/modules/subscriptions/subscriptions.repository.js
-
curl -L -o subscriptions.repository.js https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/modules/subscriptions/subscriptions.repository.js
5.39 kB
| import { prisma } from '../../config/prisma.js'; | |
| export const subscriptionsRepository = { | |
| listPlans(gymId) { | |
| return prisma.subscriptionPlan.findMany({ where: { gymId, deletedAt: null }, orderBy: { price: 'asc' } }); | |
| }, | |
| findPlan(gymId, id) { | |
| return prisma.subscriptionPlan.findFirst({ where: { id, gymId, deletedAt: null } }); | |
| }, | |
| findMember(gymId, id) { | |
| return prisma.member.findFirst({ where: { id, gymId, deletedAt: null } }); | |
| }, | |
| createPlan(gymId, data) { | |
| return prisma.subscriptionPlan.create({ data: { ...data, gymId } }); | |
| }, | |
| updatePlan(gymId, id, data) { | |
| return prisma.subscriptionPlan.update({ where: { id, gymId }, data }); | |
| }, | |
| deletePlan(gymId, id) { | |
| return prisma.subscriptionPlan.update({ where: { id, gymId }, data: { deletedAt: new Date() } }); | |
| }, | |
| list({ gymId, skip, take, search }) { | |
| const where = { | |
| gymId, | |
| deletedAt: null, | |
| ...(search | |
| ? { | |
| OR: [ | |
| { member: { fullName: { contains: search } } }, | |
| { plan: { name: { contains: search } } }, | |
| ], | |
| } | |
| : {}), | |
| }; | |
| return Promise.all([ | |
| prisma.subscription.findMany({ | |
| where, | |
| skip, | |
| take, | |
| orderBy: { createdAt: 'desc' }, | |
| include: { member: true, plan: true }, | |
| }), | |
| prisma.subscription.count({ where }), | |
| ]); | |
| }, | |
| findById(gymId, id) { | |
| return prisma.subscription.findFirst({ where: { id, gymId, deletedAt: null }, include: { member: true, plan: true } }); | |
| }, | |
| findForMember(gymId, memberId) { | |
| return prisma.subscription.findMany({ | |
| where: { gymId, memberId, deletedAt: null }, | |
| include: { member: true, plan: true }, | |
| orderBy: { endDate: 'desc' }, | |
| }); | |
| }, | |
| expiring(gymId, days = 7) { | |
| const now = new Date(); | |
| const until = new Date(now); | |
| until.setDate(until.getDate() + days); | |
| return prisma.subscription.findMany({ | |
| where: { | |
| gymId, | |
| deletedAt: null, | |
| status: 'active', | |
| endDate: { gte: now, lte: until }, | |
| }, | |
| include: { member: true, plan: true }, | |
| orderBy: { endDate: 'asc' }, | |
| }); | |
| }, | |
| create(data) { | |
| return prisma.subscription.create({ data, include: { member: true, plan: true } }); | |
| }, | |
| createMemberPurchase({ gymId, memberId, planId, startDate, endDate, discountAmount, finalPrice, paymentMethod, notes }) { | |
| return prisma.$transaction(async (tx) => { | |
| const subscription = await tx.subscription.create({ | |
| data: { | |
| gymId, | |
| memberId, | |
| planId, | |
| startDate, | |
| endDate, | |
| status: 'pending', | |
| discountAmount, | |
| finalPrice, | |
| }, | |
| include: { member: true, plan: true }, | |
| }); | |
| const payment = await tx.payment.create({ | |
| data: { | |
| gymId, | |
| memberId, | |
| subscriptionId: subscription.id, | |
| amount: finalPrice, | |
| paymentMethod, | |
| status: 'pending', | |
| notes, | |
| }, | |
| include: { member: true, subscription: true }, | |
| }); | |
| await tx.notification.create({ | |
| data: { | |
| gymId, | |
| memberId, | |
| type: 'payment', | |
| channel: 'email', | |
| message: `Your ${subscription.plan.name} membership request is waiting for gym admin approval.`, | |
| status: 'pending', | |
| }, | |
| }); | |
| return { subscription, payment }; | |
| }); | |
| }, | |
| createActiveWithPayment({ gymId, memberId, planId, startDate, endDate, discountAmount, finalPrice, paymentMethod = 'cash', notes }) { | |
| return prisma.$transaction(async (tx) => { | |
| await tx.subscription.updateMany({ | |
| where: { gymId, memberId, status: 'active', deletedAt: null }, | |
| data: { status: 'canceled' }, | |
| }); | |
| const subscription = await tx.subscription.create({ | |
| data: { | |
| gymId, | |
| memberId, | |
| planId, | |
| startDate, | |
| endDate, | |
| status: 'active', | |
| discountAmount, | |
| finalPrice, | |
| }, | |
| include: { member: true, plan: true }, | |
| }); | |
| const payment = await tx.payment.create({ | |
| data: { | |
| gymId, | |
| memberId, | |
| subscriptionId: subscription.id, | |
| amount: finalPrice, | |
| paymentMethod, | |
| status: 'success', | |
| notes, | |
| }, | |
| }); | |
| return { ...subscription, payment }; | |
| }); | |
| }, | |
| renewWithPayment(gymId, id, data, payment) { | |
| return prisma.$transaction(async (tx) => { | |
| const subscription = await tx.subscription.update({ | |
| where: { id, gymId }, | |
| data, | |
| include: { member: true, plan: true }, | |
| }); | |
| await tx.payment.create({ | |
| data: { | |
| gymId, | |
| memberId: subscription.memberId, | |
| subscriptionId: subscription.id, | |
| amount: payment.amount, | |
| paymentMethod: payment.paymentMethod || 'cash', | |
| status: 'success', | |
| notes: payment.notes, | |
| }, | |
| }); | |
| return subscription; | |
| }); | |
| }, | |
| update(gymId, id, data) { | |
| return prisma.subscription.update({ where: { id, gymId }, data, include: { member: true, plan: true } }); | |
| }, | |
| expireOld(gymId) { | |
| return prisma.subscription.updateMany({ | |
| where: { gymId, status: 'active', endDate: { lt: new Date() }, deletedAt: null }, | |
| data: { status: 'expired' }, | |
| }); | |
| }, | |
| }; | |