-
Notifications
You must be signed in to change notification settings - Fork 3
/
populate_database.ts
337 lines (306 loc) · 13 KB
/
populate_database.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import dotenv from 'dotenv';
import { randomUUID } from 'crypto';
// Models
import {
User, Course, UserType, ActivityCourse, Institution, InstitutionImage, InstitutionSocialMedia,
ActivityStatus, ActivityType, Activity, ActivityApplication, ActivityLanguage, ActivityCriteria, ActivityPartnerInstitution,
Language, Criteria, SocialMedia, Country, InstitutionCountry
} from './src/core/repositories/database/models/Models';
// Enums
import { UserTypeEnum } from './src/core/helpers/enums/UserTypeEnum';
import { ActivityTypeEnum } from './src/core/helpers/enums/ActivityTypeEnum';
import { ActivityStatusEnum } from './src/core/helpers/enums/ActivityStatusEnum';
// Mocks
import { CourseMock } from './src/core/structure/mocks/CourseMock';
import { CountryMock } from './src/core/structure/mocks/CountryMock';
import { CriteriaMock } from './src/core/structure/mocks/CriteriaMock';
import { LanguageMock } from './src/core/structure/mocks/LanguageMock';
import { SocialMediaMock } from './src/core/structure/mocks/SocialMediaMock';
// User Entity
import { User as UserEntity } from './src/core/structure/entities/User';
import { Institution as InstitutionEntity } from './src/core/structure/entities/Institution';
dotenv.config();
const stage = process.env.STAGE || "";
// Getting mocks
const users: UserEntity[] = [
new UserEntity({
id: randomUUID(),
name: "Relações Internacionais",
email: "relacoes-internacionais@maua.br",
user_type: UserTypeEnum.ADMIN,
created_at: new Date(),
updated_at: new Date()
})
];
if (stage === "dev" || stage === "test") {
users.push(new UserEntity({
id: "764f9730-433c-42df-b0bc-517fcf3778be",
name: "Felipe Carillo",
email: "23.00765-6@maua.br",
user_type: UserTypeEnum.ADMIN,
created_at: new Date(),
updated_at: new Date()
}));
}
const institutions: InstitutionEntity[] = [
new InstitutionEntity({
id: randomUUID(),
name: "Fontys University of Applied Sciences",
description: "Fontys University of Applied Sciences is a Dutch university of applied sciences with over 44,000 students in several campuses located in the southern Netherlands.",
email: "test@test.com",
countries: [
{ id: new CountryMock().countries[0].id }
],
social_medias: [
{ id: new SocialMediaMock().social_medias[0].id, link: "https://www.facebook.com/Fontys/" },
{ id: new SocialMediaMock().social_medias[1].id, link: "https://www.instagram.com/fontys/" },
],
images: [
"https://th.bing.com/th/id/OIP.3cEyScEWFEowrqodJh7LUAHaHZ?rs=1&pid=ImgDetMain"
]
}),
]
const courses: CourseMock = new CourseMock();
const countries: CountryMock = new CountryMock();
const languages: LanguageMock = new LanguageMock();
const criterias: CriteriaMock = new CriteriaMock();
const social_medias: SocialMediaMock = new SocialMediaMock();
const userTypes: UserTypeEnum[] = [
UserTypeEnum.ADMIN, UserTypeEnum.STUDENT, UserTypeEnum.MODERATOR
];
const activityStatuses: ActivityStatusEnum[] = [
ActivityStatusEnum.CANCELED, ActivityStatusEnum.ACTIVE, ActivityStatusEnum.ON_HOLD, ActivityStatusEnum.ENDED, ActivityStatusEnum.TO_START
];
const activityTypes: ActivityTypeEnum[] = [
ActivityTypeEnum.ACADEMIC_MOBILITY, ActivityTypeEnum.PROJECT
];
// Create or update the database
async function handleDatabaseCreation(): Promise<void> {
const models = [
UserType, Course, SocialMedia, Language, Country, Criteria, Institution, User,
ActivityStatus, ActivityType, InstitutionImage, InstitutionSocialMedia,
InstitutionCountry, Activity, ActivityApplication, ActivityLanguage,
ActivityCriteria, ActivityPartnerInstitution, ActivityCourse
];
for (const model of models) {
await model.sync({ alter: true });
}
console.log("Database checked/created");
}
// Create or update courses
async function handleCoursesCreation(): Promise<void> {
for (const course of courses.courses) {
let existingCourse = await Course.findOne({ where: { course: course.course } });
if (!existingCourse) {
await Course.create({ course: course.course });
console.log(`Course ${course.course} created`);
} else {
await Course.update({ course: course.course }, { where: { course: course.course } });
console.log(`Course ${course.course} updated`);
}
}
}
// Create or update languages
async function handleLanguagesCreation(): Promise<void> {
for (const language of languages.languages) {
let existingLanguage = await Language.findOne({ where: { language: language.language } });
if (!existingLanguage) {
await Language.create({ language: language.language, language_code: language.language_code });
console.log(`Language ${language.language} created`);
} else {
await Language.update({ language: language.language, language_code: language.language_code }, { where: { language: language.language } });
console.log(`Language ${language.language} updated`);
}
}
console.log("Languages checked/created");
}
// Create or update countries
async function handleCountriesCreation(): Promise<void> {
for (const country of countries.countries) {
let existingCountry = await Country.findOne({ where: { country: country.country } });
if (!existingCountry) {
await Country.create({ country: country.country, country_code: country.country_code });
console.log(`Country ${country.country} created`);
} else {
await Country.update({ country: country.country, country_code: country.country_code }, { where: { country: country.country } });
console.log(`Country ${country.country} updated`);
}
}
console.log("Countries checked/created");
}
// Create or update criterias
async function handleCriteriasCreation(): Promise<void> {
for (const criteria of criterias.criterias) {
let existingCriteria = await Criteria.findOne({ where: { criteria: criteria.criteria } });
if (!existingCriteria) {
await Criteria.create({ criteria: criteria.criteria });
console.log(`Criteria ${criteria.criteria} created`);
} else {
await Criteria.update({ criteria: criteria.criteria }, { where: { criteria: criteria.criteria } });
console.log(`Criteria ${criteria.criteria} updated`);
}
}
console.log("Criterias checked/created");
}
// Create or update enums
async function createOrUpdateEnumItems(model: any, items: any[], enumType: any): Promise<void> {
for (const item of items) {
let existingItem = await model.findOne({ where: { id: item } });
if (!existingItem) {
await model.create({ name: enumType[item] });
console.log(`${model.name} ${item} created`);
} else {
await model.update({ name: enumType[item] }, { where: { id: item } });
console.log(`${model.name} ${item} updated`);
}
}
console.log(`${model.name} checked/created`);
}
// Create or update social media
async function createOrUpdateSocialMedia(): Promise<void> {
for (const socialMedia of social_medias.social_medias) {
let existingSocialMedia = await SocialMedia.findOne({ where: { name: socialMedia.social_media } });
if (!existingSocialMedia) {
await SocialMedia.create({
name: socialMedia.social_media,
});
console.log(`Social Media ${socialMedia.social_media} created`);
} else {
await SocialMedia.update(socialMedia, { where: { name: socialMedia.social_media } });
console.log(`Social Media ${socialMedia.social_media} updated`);
}
}
console.log("Social Media checked/created");
}
async function createOrUpdateInstitution(institution: InstitutionEntity): Promise<void> {
let existingInstitution = await Institution.findOne({ where: { name: institution.name } });
if (!existingInstitution) {
await Institution.create({
id: institution.id,
name: institution.name,
description: institution.description,
email: institution.email,
created_at: new Date(),
updated_at: new Date()
});
await InstitutionCountry.create({
institution_id: institution.id,
country_id: countries.countries[0].id
});
await InstitutionSocialMedia.create({
institution_id: institution.id,
social_media_id: social_medias.social_medias[0].id,
link: institution.social_medias[0].link
});
await InstitutionImage.create({
institution_id: institution.id,
image: institution.images[0]
});
console.log(`Institution ${institution.name} created`);
} else {
await Institution.update({
name: institution.name,
description: institution.description,
email: institution.email,
updated_at: new Date()
}, { where: { name: institution.name } });
await InstitutionCountry.update({
institution_id: institution.id,
country_id: countries.countries[0].id
}, { where: { institution_id: institution.id } });
await InstitutionSocialMedia.update({
institution_id: institution.id,
social_media_id: social_medias.social_medias[0].id,
link: institution.social_medias[0].link
}, { where: { institution_id: institution.id } });
await InstitutionImage.update({
institution_id: institution.id,
image: institution.images[0]
}, { where: { institution_id: institution.id } });
console.log(`Institution ${institution.name} updated`);
}
console.log("Institutions checked/created");
}
// Create or update users
async function createOrUpdateUser(user: UserEntity): Promise<void> {
try {
let existing = await User.findOne({ where: { email: user.email } });
if (!existing) {
await User.create({
id: user.id,
name: user.name,
email: user.email,
user_type_id: user.user_type,
created_at: user.created_at,
updated_at: user.updated_at
});
console.log(`User ${user.email} created`);
} else {
await User.update({
name: user.name,
email: user.email,
user_type_id: user.user_type,
updated_at: user.updated_at
}, { where: { id: existing.toJSON().id } });
console.log(`User ${user.email} updated`);
}
console.log("Users checked/created");
} catch (error) {
console.error(`Error creating or updating user ${user.email}:`, error);
}
}
async function createOrUpdateStatusActivity(status: ActivityStatusEnum) {
try {
let existing = await ActivityStatus.findOne({ where: { id: status } });
let name: string;
switch (status) {
case ActivityStatusEnum.ACTIVE:
name = "Apply Now";
break;
case ActivityStatusEnum.CANCELED:
name = "Canceled";
break;
case ActivityStatusEnum.ENDED:
name = "Ended";
break;
case ActivityStatusEnum.ON_HOLD:
name = "Under Analysis";
break;
case ActivityStatusEnum.TO_START:
name = "Coming Soon";
break;
}
if (!existing) {
await ActivityStatus.create({ name: name });
console.log(`Activity status ${status} created`);
} else {
await ActivityStatus.update({ name: name }, { where: { id: status } });
console.log(`Activity status ${status} updated`);
}
} catch (error) {
console.error(`Error creating or updating activity status ${status}:`, error);
}
}
(async () => {
try {
await handleDatabaseCreation();
await handleCoursesCreation();
await handleLanguagesCreation();
await handleCountriesCreation();
await handleCriteriasCreation();
await createOrUpdateSocialMedia();
await createOrUpdateStatusActivity(ActivityStatusEnum.ACTIVE);
await createOrUpdateStatusActivity(ActivityStatusEnum.CANCELED);
await createOrUpdateStatusActivity(ActivityStatusEnum.ENDED);
await createOrUpdateStatusActivity(ActivityStatusEnum.ON_HOLD);
await createOrUpdateStatusActivity(ActivityStatusEnum.TO_START);
await createOrUpdateEnumItems(ActivityType, activityTypes, ActivityTypeEnum);
await createOrUpdateEnumItems(UserType, userTypes, UserTypeEnum);
await createOrUpdateInstitution(institutions[0]);
for (const user of users) {
await createOrUpdateUser(user);
}
} catch (error) {
console.error(error);
}
})();