feat(backend): migrate freelancer and portal runtimes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { and, asc, count, desc, eq, ne, sql } from "drizzle-orm";
|
||||
import { and, asc, count, desc, eq, gte, lte, ne, sql } from "drizzle-orm";
|
||||
import {
|
||||
calendarEvents,
|
||||
chatMessages,
|
||||
@@ -24,6 +24,8 @@ export function createDomainRepositories(db: DomainDatabase) {
|
||||
clients: {
|
||||
list: (scope: OwnerScope) =>
|
||||
db.select().from(clients).where(eq(clients.ownerUserId, scope.ownerUserId)).orderBy(desc(clients.updatedAt)).all(),
|
||||
recent: (scope: OwnerScope, limit: number) =>
|
||||
db.select().from(clients).where(eq(clients.ownerUserId, scope.ownerUserId)).orderBy(desc(clients.createdAt)).limit(limit).all(),
|
||||
get: (scope: OwnerScope, id: string) =>
|
||||
db.select().from(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).get(),
|
||||
getByPortalScope: (scope: ClientScope) =>
|
||||
@@ -36,18 +38,45 @@ export function createDomainRepositories(db: DomainDatabase) {
|
||||
db.delete(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).returning().get(),
|
||||
listActivities: (scope: OwnerScope, clientId: string) =>
|
||||
db.select().from(clientActivities).where(and(eq(clientActivities.ownerUserId, scope.ownerUserId), eq(clientActivities.clientId, clientId))).orderBy(desc(clientActivities.activityDate)).all(),
|
||||
listAllActivities: (scope: OwnerScope) =>
|
||||
db.select().from(clientActivities).where(eq(clientActivities.ownerUserId, scope.ownerUserId)).orderBy(desc(clientActivities.activityDate)).all(),
|
||||
createActivity: (scope: OwnerScope, value: Omit<typeof clientActivities.$inferInsert, "ownerUserId">) =>
|
||||
db.insert(clientActivities).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
|
||||
},
|
||||
projects: {
|
||||
list: (scope: OwnerScope) =>
|
||||
db.select().from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).orderBy(desc(projects.updatedAt)).all(),
|
||||
recent: (scope: OwnerScope, limit: number) =>
|
||||
db.select().from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).orderBy(desc(projects.createdAt)).limit(limit).all(),
|
||||
get: (scope: OwnerScope, id: string) =>
|
||||
db.select().from(projects).where(and(eq(projects.id, id), eq(projects.ownerUserId, scope.ownerUserId))).get(),
|
||||
getForClient: (scope: ClientScope, id: string) =>
|
||||
db.select().from(projects).where(and(eq(projects.id, id), eq(projects.clientId, scope.clientId))).get(),
|
||||
db.select({ project: projects })
|
||||
.from(projects)
|
||||
.innerJoin(
|
||||
clients,
|
||||
and(
|
||||
eq(projects.clientId, clients.id),
|
||||
eq(clients.id, scope.clientId),
|
||||
eq(clients.authUserId, scope.authUserId),
|
||||
),
|
||||
)
|
||||
.where(eq(projects.id, id))
|
||||
.get()?.project,
|
||||
listForClient: (scope: ClientScope) =>
|
||||
db.select().from(projects).where(eq(projects.clientId, scope.clientId)).orderBy(desc(projects.updatedAt)).all(),
|
||||
db.select({ project: projects })
|
||||
.from(projects)
|
||||
.innerJoin(
|
||||
clients,
|
||||
and(
|
||||
eq(projects.clientId, clients.id),
|
||||
eq(clients.id, scope.clientId),
|
||||
eq(clients.authUserId, scope.authUserId),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(projects.updatedAt))
|
||||
.all()
|
||||
.map(({ project }) => project),
|
||||
create: (scope: OwnerScope, value: Omit<typeof projects.$inferInsert, "ownerUserId">) =>
|
||||
db.insert(projects).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
|
||||
update: (scope: OwnerScope, id: string, value: Partial<typeof projects.$inferInsert>) =>
|
||||
@@ -91,6 +120,8 @@ export function createDomainRepositories(db: DomainDatabase) {
|
||||
},
|
||||
finance: {
|
||||
list: (scope: OwnerScope) => db.select().from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).orderBy(desc(financeTransactions.transactionDate)).all(),
|
||||
listInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
|
||||
db.select().from(financeTransactions).where(and(eq(financeTransactions.ownerUserId, scope.ownerUserId), gte(financeTransactions.transactionDate, startDate), lte(financeTransactions.transactionDate, endDate))).orderBy(asc(financeTransactions.transactionDate)).all(),
|
||||
get: (scope: OwnerScope, id: string) => db.select().from(financeTransactions).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).get(),
|
||||
create: (scope: OwnerScope, value: Omit<typeof financeTransactions.$inferInsert, "ownerUserId">) => db.insert(financeTransactions).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
|
||||
update: (scope: OwnerScope, id: string, value: Partial<typeof financeTransactions.$inferInsert>) => db.update(financeTransactions).set(value).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).returning().get(),
|
||||
@@ -98,16 +129,39 @@ export function createDomainRepositories(db: DomainDatabase) {
|
||||
},
|
||||
journal: {
|
||||
list: (scope: OwnerScope) => db.select().from(journalEntries).where(eq(journalEntries.ownerUserId, scope.ownerUserId)).orderBy(desc(journalEntries.entryDate)).all(),
|
||||
listInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
|
||||
db.select().from(journalEntries).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), gte(journalEntries.entryDate, startDate), lte(journalEntries.entryDate, endDate))).orderBy(asc(journalEntries.entryDate)).all(),
|
||||
getByDate: (scope: OwnerScope, entryDate: string) => db.select().from(journalEntries).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).get(),
|
||||
get: (scope: OwnerScope, id: string) => db.select().from(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).get(),
|
||||
create: (scope: OwnerScope, value: Omit<typeof journalEntries.$inferInsert, "ownerUserId">) => db.insert(journalEntries).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
|
||||
updateByDate: (scope: OwnerScope, entryDate: string, value: Partial<typeof journalEntries.$inferInsert>) => db.update(journalEntries).set(value).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).returning().get(),
|
||||
update: (scope: OwnerScope, id: string, value: Partial<typeof journalEntries.$inferInsert>) => db.update(journalEntries).set(value).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).returning().get(),
|
||||
remove: (scope: OwnerScope, id: string) => db.delete(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).returning().get(),
|
||||
},
|
||||
revisions: {
|
||||
list: (scope: OwnerScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.ownerUserId, scope.ownerUserId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(),
|
||||
updateStatus: (scope: OwnerScope, id: string, status: typeof projectRevisions.$inferInsert.status) => db.update(projectRevisions).set({ status }).where(and(eq(projectRevisions.id, id), eq(projectRevisions.ownerUserId, scope.ownerUserId))).returning().get(),
|
||||
listForClient: (scope: ClientScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.clientId, scope.clientId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(),
|
||||
listAllForClient: (scope: ClientScope) => db.select({ revision: projectRevisions })
|
||||
.from(projectRevisions)
|
||||
.innerJoin(
|
||||
projects,
|
||||
and(
|
||||
eq(projectRevisions.projectId, projects.id),
|
||||
eq(projects.clientId, scope.clientId),
|
||||
),
|
||||
)
|
||||
.innerJoin(
|
||||
clients,
|
||||
and(
|
||||
eq(projects.clientId, clients.id),
|
||||
eq(clients.authUserId, scope.authUserId),
|
||||
),
|
||||
)
|
||||
.where(eq(projectRevisions.clientId, scope.clientId))
|
||||
.orderBy(desc(projectRevisions.createdAt))
|
||||
.all()
|
||||
.map(({ revision }) => revision),
|
||||
},
|
||||
chat: {
|
||||
listSessions: (scope: OwnerScope) => db.select().from(chatSessions).where(eq(chatSessions.ownerUserId, scope.ownerUserId)).orderBy(desc(chatSessions.updatedAt)).all(),
|
||||
@@ -131,6 +185,27 @@ export function createDomainRepositories(db: DomainDatabase) {
|
||||
}).from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).get(),
|
||||
projectStatusCounts: (scope: OwnerScope) => db.select({ status: projects.status, value: count() }).from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).groupBy(projects.status).all(),
|
||||
taskStatusCounts: (scope: OwnerScope) => db.select({ status: tasks.status, value: count() }).from(tasks).where(eq(tasks.ownerUserId, scope.ownerUserId)).groupBy(tasks.status).all(),
|
||||
taskStatusCountsInRange: (scope: OwnerScope, startDate: Date, endDate: Date) =>
|
||||
db.select({ status: tasks.status, value: count() }).from(tasks).where(and(eq(tasks.ownerUserId, scope.ownerUserId), gte(tasks.updatedAt, startDate), lte(tasks.updatedAt, endDate))).groupBy(tasks.status).all(),
|
||||
projectIncomeInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
|
||||
db.select({
|
||||
projectId: projects.id,
|
||||
name: projects.name,
|
||||
amountMinor: sql<number>`coalesce(sum(${financeTransactions.amountMinor}), 0)`,
|
||||
})
|
||||
.from(financeTransactions)
|
||||
.innerJoin(projects, eq(financeTransactions.projectId, projects.id))
|
||||
.where(and(
|
||||
eq(financeTransactions.ownerUserId, scope.ownerUserId),
|
||||
eq(projects.ownerUserId, scope.ownerUserId),
|
||||
eq(financeTransactions.type, "income"),
|
||||
eq(financeTransactions.paymentStatus, "paid"),
|
||||
gte(financeTransactions.transactionDate, startDate),
|
||||
lte(financeTransactions.transactionDate, endDate),
|
||||
))
|
||||
.groupBy(projects.id, projects.name)
|
||||
.orderBy(desc(sql`sum(${financeTransactions.amountMinor})`))
|
||||
.all(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user