-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
222 lines (185 loc) · 6.86 KB
/
index.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
import { checkbox, select } from "@inquirer/prompts";
import * as fs from "fs";
import { ZodError } from "zod";
import { convertAllHTMLFilesToPDF, getDirectories } from "./lib/utils";
import { COMPANIES, TRANSACTIONS } from "./lib/variables";
import { collectWrapper } from "./scripts/collect";
import { createInvoice } from "./scripts/invoice/create";
import { logger } from "./scripts/logger";
import { uploadWrapper } from "./scripts/upload";
import { Companies, Invoice, InvoiceScheme, Order, OrderScheme } from "./types";
// TODO: PUSH THEN THROW ERROR, I DON'T WANT TO LOSE MY DATA (ZOD)
// TODO: PRINT PDF FASTER
// TODO: DELETE FOLDER AFTER 10 DAYS
// npm run start TRANSACTION_NAME
(async () => {
const args = process.argv.slice(2);
const TRANSACTION = args[0] as (typeof TRANSACTIONS)[number];
let transaction: (typeof TRANSACTIONS)[number];
if (TRANSACTION && TRANSACTIONS.includes(TRANSACTION)) {
transaction = TRANSACTION;
} else {
transaction = await select({
message: "Yapmak istediğiniz işlemi seçiniz",
choices: TRANSACTIONS.map((transaction) => ({ value: transaction })),
});
}
// TODO: Select date before the second company popup
// TODO: All
// TODO: Select specific order (by package name etc.)
if (transaction === "collectData") {
const selectedCompanies = await checkbox<Companies[number]>({
message: "Fatura bilgilerini toplamak istediğiniz şirket/leri seçiniz",
choices: COMPANIES.map((company) => ({ value: company })),
});
for (let index = 0; index < selectedCompanies.length; index++) {
const company = selectedCompanies[index];
if (!company) throw new Error("Şirket bulunmadı!");
await collectWrapper[company]();
}
} else if (transaction === "createInvoice") {
const selectedCompanies = await checkbox<Companies[number]>({
message: "Fatura oluşturmak istediğiniz şirket/leri seçiniz",
choices: COMPANIES.map((company) => ({ value: company })),
});
for (let index = 0; index < selectedCompanies.length; index++) {
const company = selectedCompanies[index];
if (!company) throw new Error("Şirket bulunmadı!");
const directories = getDirectories(`./data/${company}`).map(
(directory) => ({
value: directory,
})
);
if (directories.length === 0) {
logger.info(
"Tarih dosyası bulunmadı. İlk önce fatura bilgilerini toplamanız gerekiyor."
);
continue;
}
const date = await select({
message: `${company} şirketi için tarih seçiniz`,
choices: directories,
});
// TODO:
const constraines: "none" | "packageNumber" | "Micro" = await select({
message: "Darlama alanı seçiniz",
choices: [
{ name: "istemiyorum", value: "none" },
{ value: "packageNumber", name: "Paket numarası ile" },
{ value: "Micro", name: "Mikro ihracat" },
],
});
try {
const dataString = fs.readFileSync(
`./data/${company}/${date}/orders.json`,
"utf8"
);
const data: Order[] = JSON.parse(dataString);
OrderScheme.array().parse(data);
const constrainedData: Order[] = [];
if (constraines === "Micro") {
constrainedData.push(
...data.filter((order) => order.isExport === true)
);
// TODO: packageNumber input
} else if (constraines === "packageNumber") {
// constrainedData.push(...data.filter(order => order.isExport === true))
} else if (constraines === "none") {
constrainedData.push(...data);
}
await createInvoice({
company,
orders: constrainedData,
date,
isTestMode: false,
});
} catch (error) {
logger.error(error);
if (error instanceof ZodError) {
logger.error(error.message, error);
}
}
}
} else if (transaction === "printPDF") {
const selectedCompanies = await checkbox<Companies[number]>({
message: "PDF oluşturmak istediğiniz şirket/leri seçiniz",
choices: COMPANIES.map((company) => ({ value: company })),
});
for (let index = 0; index < selectedCompanies.length; index++) {
const company = selectedCompanies[index];
const directories = getDirectories(`./data/${company}`).map(
(directory) => ({
value: directory,
})
);
if (!company) throw new Error("Şirket bulunmadı!");
if (directories.length === 0) {
logger.info(
"Tarih/HTML dosyası bulunmadı. İlk önce fatura oluşturmanız gerekiyor."
);
continue;
}
const date = await select({
message: `${company} şirketi için tarih seçiniz`,
choices: directories,
});
try {
const pdfFolderPath = `./data/${company}/${date}/pdf/`;
const htmlFolderPath = `./data/${company}/${date}/html/`;
await convertAllHTMLFilesToPDF({ htmlFolderPath, pdfFolderPath });
} catch (error) {
logger.error(error);
}
}
} else if (transaction === "uploadInvoice") {
const selectedCompanies = await checkbox<Companies[number]>({
message: "Fatura yüklemek istediğiniz şirket/leri seçiniz",
choices: COMPANIES.map((company) => ({ value: company })),
});
for (let index = 0; index < selectedCompanies.length; index++) {
const company = selectedCompanies[index];
if (!company) throw new Error("Şirket bulunmadı!");
const directories = getDirectories(`./data/${company}`).map(
(directory) => ({
value: directory,
})
);
if (directories.length === 0) {
logger.info(
"Tarih dosyası bulunmadı. İlk önce fatura bilgilerini toplamanız gerekiyor."
);
continue;
}
const date = await select({
message: `${company} şirketi için tarih seçiniz`,
choices: directories,
});
try {
if (!fs.existsSync(`./data/${company}/${date}/pdf`)) {
logger.info(
"PDF dosyası mevcut değil. İlk önce faturaları PDF dönüştürünüz."
);
continue;
}
const dataString = fs.readFileSync(
`./data/${company}/${date}/invoices.json`,
"utf8"
);
const data: Invoice[] = JSON.parse(dataString);
InvoiceScheme.array().parse(data);
await uploadWrapper[company](date);
} catch (error) {
logger.error(error);
if (error instanceof ZodError) {
logger.error(error.message, error);
}
}
}
}
})();
// TODO: CONFIG
// TODO NOTION
// TODO: REMOVE DUPLACATED CODE
// TODO: Create invoice for 1 user or multiple users
// SAMPLE ERROR:
// https://sellerpublic-mars.trendyol.com/order-core-sellercenterordersbff-service/shipment-packages/X/customer-invoice