-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign.actions.ts
278 lines (231 loc) · 6.67 KB
/
design.actions.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"use server";
import { revalidatePath } from "next/cache";
import { FilterQuery, SortOrder, UpdateQuery } from "mongoose";
import { connectToDatabase } from "@/lib/mongoose";
import Design, { IDesign } from "@/lib/models/design.model";
import User from "@/lib/models/user.model";
export const registerDesign = async ({ title, description = "", userId }: RegisterDesignParams) => {
const response: ActionsResponse<IDesign> = {
status: false,
message: "",
data: null,
};
if (!title || !userId) {
response.message = "Please enter all the fields.";
return response;
}
try {
await connectToDatabase();
const newDesign = await Design.create({
title,
description,
creator: userId,
});
if (newDesign) {
revalidatePath(`/dashboard/recently-viewed`);
response.status = true;
response.message = "Design registered successfully.";
response.data = JSON.parse(JSON.stringify(newDesign));
} else {
response.message = "Unable to register new design.";
}
} catch (error) {
console.error("[REGISTER_DESIGN_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};
export const fetchDesign = async ({ designId, populate = true }: FetchDesignParams) => {
const response: ActionsResponse<IDesign> = {
status: false,
message: "",
data: null,
};
try {
await connectToDatabase();
let design = await Design.findById(designId);
if (design && populate) {
design = await design.populate([
{
path: "creator",
model: User,
select: "name photo",
},
{
path: "collaborators",
model: User,
select: "name photo",
},
]);
}
if (design) {
response.status = true;
response.message = "Design fetched successfully.";
response.data = JSON.parse(JSON.stringify(design));
} else {
response.message = "Unable to fetch design or design not found.";
}
} catch (error) {
console.error("[FETCH_DESIGN_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};
export const updateDesignMetadata = async ({ designId, title, description, path }: UpdateDesignMetadataParams) => {
const response: ActionsResponse<IDesign> = {
status: false,
message: "",
data: null,
};
if (!designId || !title || !path) {
response.message = "Invalid request.";
return response;
}
try {
await connectToDatabase();
let design = await Design.findById(designId);
if (design) {
const updatedDesign = await Design.findByIdAndUpdate(designId, {
title,
description,
});
if (updatedDesign) {
revalidatePath(path);
response.status = true;
response.message = "Design metadata updated successfully.";
} else {
response.message = "Unable to update design metadata.";
}
} else {
response.message = "Design not found.";
}
} catch (error) {
console.error("[UPDATE_DESIGN_METADATA_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};
export const updateCollaborators = async ({ designId, userId, action, path }: UpdateCollaboratorsParams) => {
const response: ActionsResponse<IDesign> = {
status: false,
message: "",
data: null,
};
if (!designId || !userId || !action || !path) {
response.message = "Invalid request.";
return response;
}
try {
await connectToDatabase();
const data: UpdateQuery<IDesign> =
action === "add"
? {
$push: { collaborators: userId },
}
: {
$pull: { collaborators: userId },
};
const design = await Design.findByIdAndUpdate(designId, data);
if (design) {
revalidatePath(path);
response.status = true;
response.message = "Design collaborators updated successfully.";
} else {
response.message = "Unable to update design collaborators.";
}
} catch (error) {
console.error("[UPDATE_DESIGN_COLLABORATORS_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};
export const fetchDesigns = async ({
userId,
limit = 8,
page = 1,
search = "",
order = "updatedAt",
sort = "desc",
type = "recently-viewed",
}: FetchDesignsParams) => {
const response: ActionsResponse<IDesign[]> = {
status: false,
message: "",
data: null,
totalPages: 1,
};
if (!userId) {
response.message = "Invalid request.";
return response;
}
const skip = (page - 1) * limit;
const sortBy = { [order as string]: sort };
let query: FilterQuery<IDesign> = {};
if (type === "recently-viewed") {
query.creator = userId;
} else if (type === "shared") {
query.collaborators = { $in: [userId] };
}
if (search.trim() !== "") {
const regex = new RegExp(search, "i");
query.$or = [{ title: { $regex: regex } }, { description: { $regex: regex } }];
}
try {
connectToDatabase();
const designs = await Design.find(query)
.limit(limit)
.skip(skip)
.sort(sortBy)
.populate([
{
path: "creator",
model: User,
select: "name photo",
},
{
path: "collaborators",
model: User,
select: "name photo",
},
]);
const totalDesigns = await Design.countDocuments(query);
if (designs.length > 0) {
response.status = true;
response.message = "Designs fetched successfully.";
response.data = JSON.parse(JSON.stringify(designs));
response.totalPages = Math.ceil(totalDesigns / limit);
} else {
response.message = "No designs found.";
}
} catch (error) {
console.error("[FETCH_DESIGNS_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};
export const deleteDesign = async (designId: string) => {
const response: ActionsResponse<IDesign> = {
status: false,
message: "",
data: null,
};
if (!designId) {
response.message = "Invalid request.";
return response;
}
try {
await connectToDatabase();
const deletedDesign = await Design.findByIdAndDelete(designId);
if (deletedDesign) {
revalidatePath("/dashboard/recently-viewed");
response.status = true;
response.message = "Design deleted successfully.";
} else {
response.message = "Design not found.";
}
} catch (error) {
console.error("[DELETE_DESIGN_ERROR] :>> ", error);
response.message = "Somethng went wrong. Please try again!";
}
return response;
};