-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathroute.ts
More file actions
141 lines (141 loc) · 3.79 KB
/
Copy pathroute.ts
File metadata and controls
141 lines (141 loc) · 3.79 KB
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
import { TagType } from '@/prisma/generated/client';
import { authSessionWithJWT } from '@/services/backend/auth';
import { jsonRes } from '@/services/backend/response';
import { devboxDB } from '@/services/db/init';
import { getRegionUid } from '@/utils/env';
import {
normalizeTemplateIcon,
updateTemplateRepositorySchema,
validateTemplateIcon
} from '@/utils/validate';
import { NextRequest } from 'next/server';
import { z } from 'zod';
export async function POST(req: NextRequest) {
try {
const headerList = req.headers;
const queryRaw = await req.json();
const imageHub = process.env.REGISTRY_ADDR;
if (!imageHub) {
console.log('IMAGE_HUB is not set');
return jsonRes({
code: 500
});
}
const query = updateTemplateRepositorySchema.parse(queryRaw);
const hasIconInput = query.icon !== undefined;
const iconCheck = validateTemplateIcon(query.icon);
if (!iconCheck.ok) {
return jsonRes({
code: 400,
error: iconCheck.error
});
}
const normalizedIcon = normalizeTemplateIcon(query.icon);
const { payload } = await authSessionWithJWT(headerList);
const templateRepository = await devboxDB.templateRepository.findUnique({
where: {
uid: query.uid,
organizationUid: payload.organizationUid,
isDeleted: false
},
select: {
templateRepositoryTags: {
select: {
tagUid: true
}
},
templates: {
where: {
isDeleted: false
},
select: {
uid: true,
name: true
}
}
}
});
if (!templateRepository) {
return jsonRes({
code: 404,
error: 'template repository not found'
});
}
const officialTagList = await devboxDB.tag.findMany({
where: {
type: TagType.OFFICIAL_CONTENT
},
select: {
uid: true
}
});
const originalTaglist = templateRepository.templateRepositoryTags;
const deletedTagList = originalTaglist
.filter((item) => !query.tagUidList.includes(item.tagUid))
.map((item) => item.tagUid);
const createdTagList = query.tagUidList.filter(
(item) =>
!originalTaglist.some((tag) => tag.tagUid === item) &&
!officialTagList.some((tag) => tag.uid === item)
);
const isExist = await devboxDB.templateRepository.findUnique({
where: {
isDeleted_regionUid_name: {
regionUid: getRegionUid(),
name: query.templateRepositoryName,
isDeleted: false
}
}
});
if (isExist && isExist.uid !== query.uid) {
return jsonRes({
code: 409,
error: 'template repository name already exists'
});
}
await devboxDB.$transaction(async (tx) => {
await tx.templateRepository.update({
where: {
organizationUid: payload.organizationUid,
uid: query.uid
},
data: {
description: query.description,
name: query.templateRepositoryName,
isPublic: query.isPublic,
...(hasIconInput ? { icon: normalizedIcon } : {})
}
});
await tx.templateRepositoryTag.deleteMany({
where: {
templateRepositoryUid: query.uid,
tagUid: {
in: deletedTagList
}
}
});
await tx.templateRepositoryTag.createMany({
data: createdTagList.map((tagUid) => ({
tagUid,
templateRepositoryUid: query.uid
}))
});
});
return jsonRes({
data: {
success: true
}
});
} catch (err: any) {
if (err instanceof z.ZodError) {
return jsonRes({
code: 400,
error: err.message
});
}
return jsonRes({
code: 500,
error: err
});
}
}