File size: 7,027 Bytes
a126dda
 
db2ddc0
a126dda
 
 
 
 
 
 
 
 
 
 
db2ddc0
 
 
 
 
 
 
 
 
a126dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2ddc0
 
 
 
 
a126dda
db2ddc0
 
 
 
a126dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2ddc0
a126dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2ddc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { prisma } from '../../config/database.js'
import { AppError } from '../../lib/AppError.js'
import { config } from '../../config/index.js'

export async function getProject(orderId, userId) {
  const order = await prisma.order.findUnique({
    where: { id: orderId },
    select: { userId: true },
  })
  if (!order) throw new AppError('الطلب غير موجود', 404, 'ORDER_NOT_FOUND')
  if (order.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    include: {
      template: {
        select: {
          id: true, title: true, previewUrl: true, sourceUrl: true,
          sourceFile: true, manifest: true,
          versions: { where: { isCurrent: true }, take: 1, select: { version: true } },
        },
      },
    },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')
  return project
}

export async function createProject(orderId, userId, templateId) {
  const order = await prisma.order.findUnique({
    where: { id: orderId },
    select: { userId: true, status: true },
  })
  if (!order) throw new AppError('الطلب غير موجود', 404, 'ORDER_NOT_FOUND')
  if (order.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')
  if (order.status !== 'COMPLETED') throw new AppError('الطلب لم يكتمل بعد', 400, 'ORDER_NOT_COMPLETED')

  const existing = await prisma.customerProject.findUnique({ where: { orderId } })
  if (existing) throw new AppError('المشروع موجود بالفعل', 409, 'PROJECT_EXISTS')

  const currentVersion = await prisma.templateVersion.findFirst({
    where: { templateId, isCurrent: true },
    select: { version: true },
  })

  return prisma.customerProject.create({
    data: {
      orderId, userId, templateId, config: {},
      currentVersion: currentVersion?.version || null,
    },
    include: { template: { select: { id: true, title: true, previewUrl: true } } },
  })
}

export async function updateConfig(orderId, userId, config) {
  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    select: { id: true, userId: true },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')
  if (project.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  return prisma.customerProject.update({
    where: { id: project.id },
    data: { config },
    include: { template: { select: { id: true, title: true, previewUrl: true, manifest: true } } },
  })
}

export async function updateUrls(orderId, userId, data) {
  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    select: { id: true, userId: true },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')
  if (project.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  return prisma.customerProject.update({
    where: { id: project.id },
    data,
    include: { template: { select: { id: true, title: true, previewUrl: true } } },
  })
}

export async function getSourceFile(orderId, userId) {
  const order = await prisma.order.findUnique({
    where: { id: orderId },
    select: { userId: true },
  })
  if (!order) throw new AppError('الطلب غير موجود', 404, 'ORDER_NOT_FOUND')
  if (order.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    include: { template: { select: { id: true, title: true, sourceFile: true, sourceUrl: true } } },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')

  const template = project.template
  if (!template.sourceFile) throw new AppError('ملف المصدر غير متاح', 404, 'SOURCE_NOT_FOUND')

  return { filePath: template.sourceFile, templateTitle: template.title }
}

export async function checkForUpdate(orderId, userId) {
  const order = await prisma.order.findUnique({
    where: { id: orderId },
    select: { userId: true },
  })
  if (!order) throw new AppError('الطلب غير موجود', 404, 'ORDER_NOT_FOUND')
  if (order.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    select: { currentVersion: true },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')

  const orderWithTemplate = await prisma.order.findUnique({
    where: { id: orderId },
    select: { templateId: true },
  })

  const latest = await prisma.templateVersion.findFirst({
    where: { templateId: orderWithTemplate.templateId, channel: 'stable' },
    orderBy: { createdAt: 'desc' },
    select: { version: true, changelog: true, createdAt: true },
  })

  if (!latest) return { updateAvailable: false, currentVersion: project.currentVersion, latestVersion: null }

  const updateAvailable = project.currentVersion !== latest.version

  return {
    updateAvailable,
    currentVersion: project.currentVersion,
    latestVersion: latest.version,
    changelog: latest.changelog,
    releasedAt: latest.createdAt,
  }
}

export async function applyTemplateUpdate(orderId, userId) {
  const order = await prisma.order.findUnique({
    where: { id: orderId },
    select: { userId: true, templateId: true },
  })
  if (!order) throw new AppError('الطلب غير موجود', 404, 'ORDER_NOT_FOUND')
  if (order.userId !== userId) throw new AppError('ليس لديك صلاحية', 403, 'FORBIDDEN')

  const latest = await prisma.templateVersion.findFirst({
    where: { templateId: order.templateId, channel: 'stable' },
    orderBy: { createdAt: 'desc' },
    select: { version: true },
  })
  if (!latest) throw new AppError('لا توجد إصدارات متاحة', 404, 'NO_VERSIONS')

  const project = await prisma.customerProject.findUnique({
    where: { orderId },
    select: { id: true, currentVersion: true },
  })
  if (!project) throw new AppError('المشروع غير موجود', 404, 'PROJECT_NOT_FOUND')
  if (project.currentVersion === latest.version) throw new AppError('المشروع بالفعل على أحدث إصدار', 400, 'ALREADY_UPDATED')

  return prisma.customerProject.update({
    where: { id: project.id },
    data: { currentVersion: latest.version, updateStatus: 'up_to_date', availableVersion: null },
    include: { template: { select: { id: true, title: true } } },
  })
}

export async function generatePreviewUrl(orderId, userId) {
  const project = await getProject(orderId, userId)
  const baseUrl = config.FRONTEND_URL || ''
  const params = new URLSearchParams({ config: JSON.stringify(project.config) })
  return `${baseUrl}/api/preview/${project.templateId}?${params.toString()}`
}