Skip to content

Commit 218c66d

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents fd526f8 + 5d6b6f1 commit 218c66d

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/db/upload.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,17 @@ export const checkUploadsStatus = async (uploadsId: string[]) => {
135135

136136
return Promise.all(
137137
uploads
138-
.filter((upload) =>
139-
upload.products.every((product) => product.status === Status.Done || product.status === Status.Error),
138+
.filter(
139+
(upload) =>
140+
upload.products.every((product) => product.status === Status.Done || product.status === Status.Error) &&
141+
upload.reUploadProducts.every(
142+
(product) => product.product.status === Status.Done || product.product.status === Status.Error,
143+
),
140144
)
141145
.map(async (upload) => {
142-
const allDone = upload.products.every((product) => product.status === Status.Done)
146+
const allDone =
147+
upload.products.every((product) => product.status === Status.Done) &&
148+
upload.reUploadProducts.every((product) => product.product.status === Status.Done)
143149
if (allDone) {
144150
return completeUpload(upload)
145151
} else {

src/scripts/generateCSV.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const generate = (name: string, length?: string) => {
4444
countryMaking: faker.helpers.arrayElement(Object.keys(countries)),
4545
countrySpinning: faker.helpers.arrayElement(Object.keys(countries)),
4646
impressionMapping: faker.helpers.arrayElement(["", ...Object.keys(impressionMapping)]),
47-
impressionPercentage: faker.number.float({ min: 0, max: 1 }),
47+
impressionPercentage: faker.number.float({ min: 0, max: 0.8 }),
4848
mass: faker.number.float({ min: 0.01 }),
4949
price: faker.number.float({ min: 1, max: 1000 }),
5050
airTransportRatio: faker.number.float({ min: 0, max: 1 }),

src/utils/queue/uploads.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chardet from "chardet"
33
import { parseCSV } from "../csv/parse"
44
import { createProducts } from "../../db/product"
55
import { failUpload, completeUpload } from "../../services/upload"
6-
import { getFirstFileUpload, updateUploadToPending } from "../../db/upload"
6+
import { checkUploadsStatus, getFirstFileUpload, updateUploadToPending } from "../../db/upload"
77
import { downloadFileFromS3 } from "../s3/bucket"
88
import { FileUpload } from "../../db/upload"
99
import { Status } from "../../../prisma/src/prisma"
@@ -22,6 +22,7 @@ const mockedParseCSV = parseCSV as jest.MockedFunction<typeof parseCSV>
2222
const mockedCreateProducts = createProducts as jest.MockedFunction<typeof createProducts>
2323
const mockedFailUpload = failUpload as jest.MockedFunction<typeof failUpload>
2424
const mockedCompleteUpload = completeUpload as jest.MockedFunction<typeof completeUpload>
25+
const mockedCheckUploadStatus = checkUploadsStatus as jest.MockedFunction<typeof checkUploadsStatus>
2526
const mockedGetFirstFileUpload = getFirstFileUpload as jest.MockedFunction<typeof getFirstFileUpload>
2627
const mockedUpdateUploadToPending = updateUploadToPending as jest.MockedFunction<typeof updateUploadToPending>
2728
const mockedDownloadFileFromS3 = downloadFileFromS3 as jest.MockedFunction<typeof downloadFileFromS3>
@@ -43,6 +44,7 @@ describe("processUploadsToQueue", () => {
4344
},
4445
},
4546
products: [{ status: Status.Pending }],
47+
reUploadProducts: [],
4648
} satisfies FileUpload
4749

4850
const mockBuffer = Buffer.from("csv,data,here")
@@ -130,7 +132,7 @@ describe("processUploadsToQueue", () => {
130132
expect(mockedParseCSV).toHaveBeenCalledWith(mockBuffer, "latin1", mockUpload)
131133
})
132134

133-
it("should complete upload when 0 products are created", async () => {
135+
it("should check upload status when 0 products are created", async () => {
134136
const mockEmptyParsedData = {
135137
products: [],
136138
materials: [],
@@ -153,7 +155,7 @@ describe("processUploadsToQueue", () => {
153155
expect(mockedChardet.detect).toHaveBeenCalledWith(mockBuffer)
154156
expect(mockedParseCSV).toHaveBeenCalledWith(mockBuffer, "utf-8", mockUpload)
155157
expect(mockedCreateProducts).toHaveBeenCalledWith(mockEmptyParsedData)
156-
expect(mockedCompleteUpload).toHaveBeenCalledWith(mockUpload)
158+
expect(mockedCheckUploadStatus).toHaveBeenCalledWith([mockUpload.id])
157159
expect(mockedFailUpload).not.toHaveBeenCalled()
158160
})
159161

src/utils/queue/uploads.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import chardet from "chardet"
22
import { parseCSV } from "../csv/parse"
33
import { createProducts } from "../../db/product"
4-
import { completeUpload, failUpload } from "../../services/upload"
5-
import { getFirstFileUpload, updateUploadToPending } from "../../db/upload"
4+
import { failUpload } from "../../services/upload"
5+
import { checkUploadsStatus, getFirstFileUpload, updateUploadToPending } from "../../db/upload"
66
import { downloadFileFromS3 } from "../s3/bucket"
77
import { decryptAndDezipFile } from "../encryption/encryption"
88

@@ -39,7 +39,7 @@ export const processUploadsToQueue = async () => {
3939
const csvData = await parseCSV(buffer, encoding, upload)
4040
const numberOfCreatedProduct = await createProducts(csvData)
4141
if (numberOfCreatedProduct === 0) {
42-
await completeUpload(upload)
42+
await checkUploadsStatus([upload.id])
4343
}
4444
console.log(`Upload processed, ${csvData.products.length} products, ${numberOfCreatedProduct} created`)
4545
} catch (error) {

0 commit comments

Comments
 (0)