Skip to content

Commit

Permalink
Migrate sales to earnings model and update related routes
Browse files Browse the repository at this point in the history
  • Loading branch information
devkiran committed Feb 4, 2025
1 parent f3eaedd commit fd10e45
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
14 changes: 10 additions & 4 deletions apps/web/app/api/programs/[programId]/metrics/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ export const GET = withWorkspace(
},
}),

prisma.sale.aggregate({
where,
prisma.earnings.aggregate({
where: {
...where,
type: "sale",
},
_sum: {
amount: true,
},
}),

prisma.sale.count({
where,
prisma.earnings.count({
where: {
...where,
type: "sale",
},
}),

prisma.programEnrollment.count({
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/programs/[programId]/sales/amount/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export const GET = withWorkspace(

const { partnerId } = parsed;

const salesAmount = await prisma.sale.aggregate({
const salesAmount = await prisma.earnings.aggregate({
where: {
type: "sale",
programId,
partnerId,
createdAt: {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/programs/[programId]/sales/count/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export const GET = withWorkspace(

const { startDate, endDate } = getStartEndDates(parsed);

const salesCount = await prisma.sale.groupBy({
const salesCount = await prisma.earnings.groupBy({
by: ["status"],
where: {
type: "sale",
programId,
status,
partnerId,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/programs/[programId]/sales/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export const GET = withWorkspace(
programId,
});

const sales = await prisma.sale.findMany({
const sales = await prisma.earnings.findMany({
where: {
programId,
type: "sale",
...(status && { status }),
...(customerId && { customerId }),
...(payoutId && { payoutId }),
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/actions/partners/update-sale-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const updateSaleStatusAction = authActionClient
const { workspace } = ctx;
const { saleId, status } = parsedInput;

const sale = await prisma.sale.findUniqueOrThrow({
const sale = await prisma.earnings.findUniqueOrThrow({
where: {
id: saleId,
program: {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const updateSaleStatusAction = authActionClient
});
}

await prisma.sale.update({
await prisma.earnings.update({
where: {
id: sale.id,
},
Expand Down
4 changes: 3 additions & 1 deletion apps/web/scripts/migrate-sales.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "dotenv-flow/config";
import { prisma } from "@dub/prisma";
import { EventType } from "@dub/prisma/client";
import "dotenv-flow/config";

async function main() {
const sales = await prisma.sale.findMany({
select: {
id: true,
programId: true,
partnerId: true,
linkId: true,
Expand All @@ -14,6 +15,7 @@ async function main() {
eventId: true,
amount: true,
earnings: true,
currency: true,
status: true,
createdAt: true,
updatedAt: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/prisma/schema/customer.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ model Customer {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
link Link? @relation(fields: [linkId], references: [id])
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
link Link? @relation(fields: [linkId], references: [id])
sales Sale[]
earnings Earnings[]
Expand Down
4 changes: 2 additions & 2 deletions packages/prisma/schema/earnings.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ model Earnings {
type EventType
amount Int?
quantity Int
earnings Int?
// currency String
earnings Int @default(0)
currency String?
status EarningsStatus @default(pending)
createdAt DateTime @default(now())
Expand Down

0 comments on commit fd10e45

Please sign in to comment.