|
|
@@ -0,0 +1,87 @@
|
|
|
+import { Injectable } from '@nestjs/common'
|
|
|
+import { db } from '../../db/database.module'
|
|
|
+import { projects } from '../../db/schema'
|
|
|
+import { and, desc, eq } from 'drizzle-orm'
|
|
|
+import { nanoid } from 'nanoid'
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class ProjectsRepository {
|
|
|
+ async create(data: {
|
|
|
+ orgId: string
|
|
|
+ name: string
|
|
|
+ description?: string
|
|
|
+ timezone?: string
|
|
|
+ startDate?: string
|
|
|
+ endDate?: string
|
|
|
+ status?: 'planning' | 'active' | 'paused' | 'completed' | 'archived'
|
|
|
+ captureInterval?: number
|
|
|
+ resolution?: string
|
|
|
+ }) {
|
|
|
+ const id = nanoid()
|
|
|
+ await db.insert(projects).values({
|
|
|
+ id,
|
|
|
+ orgId: data.orgId,
|
|
|
+ name: data.name,
|
|
|
+ description: data.description ?? null,
|
|
|
+ timezone: data.timezone ?? 'Asia/Ho_Chi_Minh',
|
|
|
+ startDate: data.startDate ? new Date(data.startDate) : null,
|
|
|
+ endDate: data.endDate ? new Date(data.endDate) : null,
|
|
|
+ status: data.status ?? 'planning',
|
|
|
+ captureInterval: data.captureInterval ?? 60,
|
|
|
+ resolution: data.resolution ?? '1920x1080',
|
|
|
+ })
|
|
|
+ return this.findById(id)
|
|
|
+ }
|
|
|
+
|
|
|
+ async findById(id: string) {
|
|
|
+ const result = await db.select().from(projects).where(eq(projects.id, id)).limit(1)
|
|
|
+ return result[0] ?? null
|
|
|
+ }
|
|
|
+
|
|
|
+ async findAll(orgId?: string) {
|
|
|
+ if (orgId) {
|
|
|
+ return db
|
|
|
+ .select()
|
|
|
+ .from(projects)
|
|
|
+ .where(eq(projects.orgId, orgId))
|
|
|
+ .orderBy(desc(projects.updatedAt))
|
|
|
+ }
|
|
|
+
|
|
|
+ return db.select().from(projects).orderBy(desc(projects.updatedAt))
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(
|
|
|
+ id: string,
|
|
|
+ data: Partial<{
|
|
|
+ name: string
|
|
|
+ description: string
|
|
|
+ timezone: string
|
|
|
+ startDate: string
|
|
|
+ endDate: string
|
|
|
+ status: 'planning' | 'active' | 'paused' | 'completed' | 'archived'
|
|
|
+ captureInterval: number
|
|
|
+ resolution: string
|
|
|
+ }>,
|
|
|
+ ) {
|
|
|
+ await db
|
|
|
+ .update(projects)
|
|
|
+ .set({
|
|
|
+ ...(data.name !== undefined && { name: data.name }),
|
|
|
+ ...(data.description !== undefined && { description: data.description }),
|
|
|
+ ...(data.timezone !== undefined && { timezone: data.timezone }),
|
|
|
+ ...(data.startDate !== undefined && { startDate: data.startDate ? new Date(data.startDate) : null }),
|
|
|
+ ...(data.endDate !== undefined && { endDate: data.endDate ? new Date(data.endDate) : null }),
|
|
|
+ ...(data.status !== undefined && { status: data.status }),
|
|
|
+ ...(data.captureInterval !== undefined && { captureInterval: data.captureInterval }),
|
|
|
+ ...(data.resolution !== undefined && { resolution: data.resolution }),
|
|
|
+ updatedAt: new Date(),
|
|
|
+ })
|
|
|
+ .where(eq(projects.id, id))
|
|
|
+
|
|
|
+ return this.findById(id)
|
|
|
+ }
|
|
|
+
|
|
|
+ async remove(id: string) {
|
|
|
+ await db.delete(projects).where(eq(projects.id, id))
|
|
|
+ }
|
|
|
+}
|