Skip to content

Commit 3f82850

Browse files
Fix/profile general (#486)
* fix/profile-general: fixed fe-be * fix/profile-general: prettier
1 parent dbd9417 commit 3f82850

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+717
-148
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- DropForeignKey
2+
ALTER TABLE "Chat" DROP CONSTRAINT "Chat_productId_fkey";
3+
4+
-- DropForeignKey
5+
ALTER TABLE "ChatMember" DROP CONSTRAINT "ChatMember_chatId_fkey";
6+
7+
-- DropForeignKey
8+
ALTER TABLE "FavoriteProducts" DROP CONSTRAINT "FavoriteProducts_productId_fkey";
9+
10+
-- DropForeignKey
11+
ALTER TABLE "Message" DROP CONSTRAINT "Message_chatId_fkey";
12+
13+
-- AddForeignKey
14+
ALTER TABLE "Chat" ADD CONSTRAINT "Chat_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;
15+
16+
-- AddForeignKey
17+
ALTER TABLE "ChatMember" ADD CONSTRAINT "ChatMember_chatId_fkey" FOREIGN KEY ("chatId") REFERENCES "Chat"("id") ON DELETE CASCADE ON UPDATE CASCADE;
18+
19+
-- AddForeignKey
20+
ALTER TABLE "Message" ADD CONSTRAINT "Message_chatId_fkey" FOREIGN KEY ("chatId") REFERENCES "Chat"("id") ON DELETE CASCADE ON UPDATE CASCADE;
21+
22+
-- AddForeignKey
23+
ALTER TABLE "FavoriteProducts" ADD CONSTRAINT "FavoriteProducts_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;

packages/backend/prisma/schema.prisma

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ model Order {
153153
model Chat {
154154
id String @id @default(uuid()) @db.Uuid
155155
title String @db.VarChar(256)
156-
product Product @relation(fields: [productId], references: [id])
156+
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
157157
productId String @unique @db.Uuid
158158
members ChatMember[]
159159
messages Message[]
@@ -166,7 +166,7 @@ model ChatMember {
166166
id String @id @default(uuid()) @db.Uuid
167167
user User @relation(fields: [userId], references: [id])
168168
userId String @db.Uuid
169-
chat Chat @relation(fields: [chatId], references: [id])
169+
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
170170
chatId String @db.Uuid
171171
createdAt DateTime @default(now())
172172
updatedAt DateTime @updatedAt
@@ -176,7 +176,7 @@ model Message {
176176
id String @id @default(uuid()) @db.Uuid
177177
sender User @relation(fields: [senderId], references: [id])
178178
senderId String @db.Uuid
179-
chat Chat @relation(fields: [chatId], references: [id])
179+
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
180180
chatId String @db.Uuid
181181
text String
182182
createdAt DateTime @default(now())
@@ -205,7 +205,7 @@ model FavoriteProducts {
205205
userId String @db.Uuid
206206
user User @relation(fields: [userId], references: [id])
207207
productId String @db.Uuid
208-
product Product @relation(fields: [productId], references: [id])
208+
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
209209
210210
@@id([userId, productId])
211211
}

packages/backend/src/api/routes/profile.ts

+49-5
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,11 @@ export const initProfileRoutes = (
189189
userAddress,
190190
});
191191

192-
await profileService.updateUserSocialMedia({
192+
const links = await profileService.updateUserSocialMedia({
193193
userId,
194194
socialMedia,
195195
});
196196

197-
const links = await profileService.getSocialMedia({ userId });
198-
199197
if (newPassword) {
200198
await profileService.changePassword({
201199
userId,
@@ -268,15 +266,15 @@ export const initProfileRoutes = (
268266
authMiddleware,
269267
wrap(async (req: Request) => {
270268
const { userId } = req;
271-
const { itemId, endDate } = req.body;
269+
const { itemId, updatedAt } = req.body;
272270
await profileService.getUser({
273271
userId,
274272
});
275273
await productService.getById(itemId);
276274

277275
return await myListService.addItemToArchive({
278276
itemId,
279-
endDate,
277+
updatedAt,
280278
});
281279
}),
282280
);
@@ -327,6 +325,52 @@ export const initProfileRoutes = (
327325
}),
328326
);
329327

328+
/**
329+
* @openapi
330+
* /profile/delete-item:
331+
* delete:
332+
* description: Remove a user's product
333+
* security:
334+
* - Bearer: []
335+
* tags: [Profile]
336+
* parameters:
337+
* - in: path
338+
* name: DeleteProduct
339+
* required: true
340+
* type: string
341+
* produces:
342+
* - application/json
343+
* responses:
344+
* 200:
345+
* description: Ok
346+
* content:
347+
* application/json:
348+
* schema:
349+
* type: object
350+
* $ref: "#/definitions/DeleteProduct"
351+
* 4**:
352+
* description: Something went wrong
353+
* content:
354+
* application/json:
355+
* schema:
356+
* $ref: "#/definitions/Response400"
357+
*/
358+
router.delete(
359+
apiPath(path, `${ProfileApiRoutes.DELETE_ITEM}`),
360+
authMiddleware,
361+
wrap(async (req: Request) => {
362+
const itemId = <string>req.query.productId;
363+
await productService.getById(itemId);
364+
const productId = await myListService.deleteProduct({
365+
productId: itemId,
366+
});
367+
368+
return {
369+
productId,
370+
};
371+
}),
372+
);
373+
330374
/**
331375
* @openapi
332376
* /profile/:userId:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
33
"cancelledExists": "The product has already added to the archive",
4-
"postedExistes": "The product has already posted"
4+
"postedExistes": "The product has already posted",
5+
"deleteStatus": "Can not delete this product type"
56
}
67
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
33
"cancelledExists": "Товар вже доданий до архіву",
4-
"postedExists": "Товар вже опублікований"
4+
"postedExists": "Товар вже опублікований",
5+
"deleteStatus": "Неможливо видалити товар даного типу"
56
}
67
}

packages/backend/src/repositories/my-list.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { Prisma, PrismaClient, PrismaPromise } from '@prisma/client';
1+
import type {
2+
Prisma,
3+
PrismaClient,
4+
PrismaPromise,
5+
Product,
6+
} from '@prisma/client';
27
import { ProductStatus } from '@prisma/client';
38
import { Order } from '@vse-bude/shared';
49
import type { Item } from 'common/types/my-list-items';
@@ -44,10 +49,10 @@ export class MyListRepository {
4449
lastName: true,
4550
},
4651
},
47-
endDate: true,
52+
updatedAt: true,
4853
},
4954
orderBy: {
50-
endDate: Order.DESC,
55+
updatedAt: Order.DESC,
5156
},
5257
});
5358
}
@@ -116,10 +121,10 @@ export class MyListRepository {
116121
type: true,
117122
status: true,
118123
views: true,
119-
endDate: true,
124+
updatedAt: true,
120125
},
121126
orderBy: {
122-
endDate: Order.DESC,
127+
updatedAt: Order.DESC,
123128
},
124129
});
125130
}
@@ -155,10 +160,10 @@ export class MyListRepository {
155160

156161
public addItemToArchive({
157162
itemId,
158-
endDate,
163+
updatedAt,
159164
}: {
160165
itemId: string;
161-
endDate: string;
166+
updatedAt: string;
162167
}): PrismaPromise<Item> {
163168
this._deleteBids({ itemId });
164169

@@ -168,7 +173,7 @@ export class MyListRepository {
168173
},
169174
data: {
170175
status: ProductStatus.CANCELLED,
171-
endDate,
176+
updatedAt,
172177
},
173178
select: {
174179
id: true,
@@ -179,7 +184,7 @@ export class MyListRepository {
179184
type: true,
180185
status: true,
181186
views: true,
182-
endDate: true,
187+
updatedAt: true,
183188
},
184189
});
185190
}
@@ -201,4 +206,12 @@ export class MyListRepository {
201206
},
202207
});
203208
}
209+
210+
public deleteProduct({ productId }: { productId: string }): Promise<Product> {
211+
return this._dbClient.product.delete({
212+
where: {
213+
id: productId,
214+
},
215+
});
216+
}
204217
}

packages/backend/src/repositories/order.ts

+17
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ export class OrderRepository {
6060
});
6161
}
6262

63+
public getPurchasedItemById({ productId }: { productId: string }): Promise<{
64+
product: {
65+
title: string;
66+
};
67+
}> {
68+
return this._dbClient.order.findFirst({
69+
where: { productId, status: OrderStatus.PAID },
70+
select: {
71+
product: {
72+
select: {
73+
title: true,
74+
},
75+
},
76+
},
77+
});
78+
}
79+
6380
public getById(id: string): Promise<OrderById> {
6481
return this._dbClient.order.findUnique({
6582
where: { id },

packages/backend/src/repositories/profile.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,43 @@ export class UserProfileRepository {
217217
});
218218
}
219219

220-
public updateAddress({
220+
public async updateAddress({
221221
userId,
222222
data,
223223
}: {
224224
userId: string;
225225
data: UserAddressDto;
226226
}): Promise<AddressDto> {
227227
const { country, region, city, zip, deliveryData } = data;
228-
229-
return this._dbClient.address.upsert({
228+
const addressDB = await this._dbClient.address.findFirst({
230229
where: {
231230
userId,
232231
},
233-
update: {
234-
country,
235-
region,
236-
city,
237-
zip,
238-
deliveryData,
239-
},
240-
create: {
232+
});
233+
if (addressDB) {
234+
return this._dbClient.address.update({
235+
where: {
236+
userId,
237+
},
238+
data: {
239+
country,
240+
region,
241+
city,
242+
zip,
243+
deliveryData,
244+
},
245+
select: {
246+
country: true,
247+
region: true,
248+
city: true,
249+
zip: true,
250+
deliveryData: true,
251+
},
252+
});
253+
}
254+
255+
return this._dbClient.address.create({
256+
data: {
241257
country,
242258
region,
243259
city,
@@ -261,19 +277,23 @@ export class UserProfileRepository {
261277
userId: string;
262278
socialMedia: SocialMedia[];
263279
}): Promise<SocialNet>[] {
264-
return socialMedia.map((net) => {
280+
return socialMedia.map(async (net) => {
265281
const { id, link, socialMedia } = net;
266282
if (id && link) {
267-
return this._updateSocialMediaLinks({ id, link });
283+
return await this._updateSocialMediaLinks({ id, link });
268284
} else if (!id && link) {
269285
this._isSocialNetTypeExists({
270286
userId,
271287
socialMedia,
272288
});
273289

274-
return this._createSocialMediaLinks({ link, socialMedia, userId });
290+
return await this._createSocialMediaLinks({
291+
link,
292+
socialMedia,
293+
userId,
294+
});
275295
} else if (id && !link) {
276-
return this._deleteSocialMediaLinks({ id });
296+
await this._deleteSocialMediaLinks({ id });
277297
}
278298
});
279299
}

packages/backend/src/services/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export const initServices = (repositories: Repositories): ServicesInit => {
9292
myListService: new MyListService({
9393
myListRepository: repositories.myListRepository,
9494
orderRepository: repositories.orderRepository,
95+
s3StorageService,
96+
productRepository: repositories.productRepository,
9597
}),
9698
orderService: new OrderService(
9799
repositories.orderRepository,

0 commit comments

Comments
 (0)