vendor.js
8.22 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
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
"use strict";
/**
* vendor service
*/
const { createCoreService } = require("@strapi/strapi").factories;
const { factories } = require("@strapi/strapi");
const fs = require("fs");
const { parse } = require("csv-parse");
module.exports = createCoreService(
"api::vendor.vendor",
({ strapi: Strapi }) => ({
async importCountrySeedData() {
const countryData = await fs.promises.readFile(
`${__dirname}/../seed-data/country.json`,
"utf8"
);
const countries = JSON.parse(countryData);
// console.log(countries);
// get rid of all existing countries.
const deletedCnt = await strapi.db
.query("api::master-country.master-country")
.deleteMany({ where: {} });
// console.log(`Bulk deleted existing countries, got rid of: ${deletedCnt.count} country records.`);
// re-create them again.
for (let i = 0; i < countries.length; i++) {
const country = countries[i];
// console.log(`Attempting to create country: `, country);
await strapi.entityService.create(
"api::master-country.master-country",
{
data: {
name: country.name,
},
}
);
}
},
async importStateSeedData() {
const stateData = await fs.promises.readFile(
`${__dirname}/../seed-data/state.json`,
"utf8"
);
const states = JSON.parse(stateData);
// console.log(states);
// get rid of all existing states.
const deletedCnt = await strapi.db
.query("api::master-state.master-state")
.deleteMany({ where: {} });
// console.log(`Bulk deleted existing states, got rid of: ${deletedCnt.count} state records.`);
// re-create them again.
for (let i = 0; i < states.length; i++) {
const state = states[i];
const matchingCountries = await strapi.entityService.findMany(
"api::master-country.master-country",
{
fields: ["id"],
filters: { name: state.country },
}
);
let statefinal;
if (!matchingCountries || matchingCountries.length === 0) {
console.log(
`Unable to resolve a country with name ${
state.country
}, skipping creation of state ${JSON.stringify(state)}`
);
} else {
// console.log(`Attempting to create state: `, state);
statefinal = await strapi.entityService.create(
"api::master-state.master-state",
{
data: {
name: state.name,
masterCountry: matchingCountries[0].id,
},
}
);
// console.log("state", statefinal);
}
}
},
async importCitySeedData() {
const cityData = await fs.promises.readFile(
`${__dirname}/../seed-data/city.json`,
"utf8"
);
const cities = JSON.parse(cityData);
// console.log(citys);
// get rid of all existing citys.
const deletedCnt = await strapi.db
.query("api::master-city.master-city")
.deleteMany({ where: {} });
// console.log(`Bulk deleted existing citys, got rid of: ${deletedCnt.count} city records.`);
// re-create them again.
for (let i = 0; i < cities.length; i++) {
const city = cities[i];
const matchingStates = await strapi.entityService.findMany(
"api::master-state.master-state",
{
fields: ["id"],
filters: { name: city.state },
}
);
if (!matchingStates || matchingStates.length === 0) {
console.log(
`Unable to resolve a state with name ${
city.state
}, skipping creation of city ${JSON.stringify(city)}`
);
} else {
// console.log(`Attempting to create city: `, city);
await strapi.entityService.create("api::master-city.master-city", {
data: {
name: city.name,
masterState: matchingStates[0].id,
},
});
}
}
},
async importPincodeSeedData() {
// const pincodeData = await fs.readFile(`${__dirname}/../seed-data/StateCityPinMasterSeeder.csv`);
// const records = parse(fileContent, );
// const pincodes = parse(pincodeData,{columns: true});
// console.log(citys);
let pincodeData = [];
const deletedCnt = await strapi.db
.query("api::master-pincode.master-pincode")
.deleteMany({ where: {} });
const deletedStateCnt = await strapi.db
.query("api::master-state.master-state")
.deleteMany({ where: {} });
const deletedCityCnt = await strapi.db
.query("api::master-city.master-city")
.deleteMany({ where: {} });
// const pincodeData = fs.createReadStream(`${__dirname}/../seed-data/StateCityPinMasterSeeder.csv`).pipe(parse({ delimiter: ",", from_line: 1 }));
fs.createReadStream(
`${__dirname}/../seed-data/StateCityPinMasterSeeder.csv`
)
.pipe(parse({}))
.on("data", (data) => {
// console.log("data", data);
pincodeData.push(data);
})
.on("end", () => {
resolveSeedData(pincodeData);
});
const resolveSeedData = async (seedData) => {
console.log("pincode data", seedData);
for (let i = 0; i < seedData.length; i++) {
const singleData = seedData[i];
const pincode = singleData[0];
const city = singleData[1];
const state = singleData[2];
let stateId;
let cityId;
console.log("pincode", pincode);
console.log("city", city);
console.log("state", state);
const resolveCountry = await strapi
.query("api::master-country.master-country")
.findOne({
where: {
$and: [{ name: "India" }],
},
});
console.log("resolveCountry", resolveCountry);
let resolveState = await strapi
.query("api::master-state.master-state")
.findOne({
where: {
$and: [{ name: state }],
},
});
console.log("resolveState", resolveState);
if (resolveState == null) {
resolveState = await strapi.entityService.create(
"api::master-state.master-state",
{
data: {
name: state,
masterCountry: resolveCountry.id,
},
}
);
console.log("resolveState", resolveState);
stateId = resolveState.id;
} else {
stateId = resolveState.id;
}
let resolveCity = await strapi
.query("api::master-city.master-city")
.findOne({
where: {
$and: [{ name: city, masterState: stateId }],
},
});
console.log("resolveCity", resolveCity);
if (resolveCity == null) {
resolveCity = await strapi.entityService.create(
"api::master-city.master-city",
{
data: {
name: city,
masterState: stateId,
},
}
);
console.log("resolveCity", resolveCity);
cityId = resolveCity.id;
} else {
cityId = resolveCity.id;
}
let resolvePincode = await strapi
.query("api::master-pincode.master-pincode")
.findOne({
where: {
$and: [{ name: state }],
},
});
let currentCity = await strapi
.query("api::master-city.master-city")
.findOne({
where: {
$and: [{ name: city }],
},
});
if (resolvePincode == null) {
resolvePincode = await strapi.entityService.create(
"api::master-pincode.master-pincode",
{
data: {
name: pincode,
masterCity: currentCity.id,
},
}
);
}
}
};
},
})
);