Skip to content

Commit 5a85e8f

Browse files
committed
Migrate update scripts and delete prisma1
1 parent dfd7f9e commit 5a85e8f

35 files changed

+103
-22803
lines changed

back/package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"watch-graphql": "npm run graphql-codegen -- --watch",
1111
"start": "node src/index.js",
1212
"update": "node prisma/scripts/index.js",
13-
"update:dev": "ts-node prisma/scripts/index.ts",
13+
"update:dev": "ts-node -r tsconfig-paths/register prisma/scripts/index.ts",
1414
"test": "jest",
1515
"build": "tsc && copyfiles -e **/*.ts src/**/*.{graphql,pdf,png,ttf} prisma/**/*.{yml,prisma} dist/",
1616
"lint": "eslint -c .eslintrc.js --ext .ts ./src",
@@ -64,7 +64,6 @@
6464
"passport-local": "^1.0.0",
6565
"passport-oauth2-client-password": "^0.1.2",
6666
"pdf-lib": "^1.12.0",
67-
"prisma-client-lib": "1.34.10",
6867
"rate-limit-redis": "^2.0.0",
6968
"request": "^2.88.2",
7069
"winston": "^3.2.1",
@@ -119,7 +118,6 @@
119118
"jsonwebtoken": "^8.5.1",
120119
"nodemon": "^2.0.4",
121120
"prettier": "^2.0.5",
122-
"prisma1": "^1.34.11",
123121
"supertest": "^4.0.2",
124122
"ts-jest": "26.0.0",
125123
"ts-node": "^8.10.2",

back/prisma1/scripts/accept-pending-invitations.ts renamed to back/prisma/scripts/accept-pending-invitations.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Updater, registerUpdater } from "./helper/helper";
2-
import acceptPendingInvitations from "../../src/scripts/prisma/acceptPendingInvitations";
1+
import acceptPendingInvitations from "src/scripts/prisma/acceptPendingInvitations";
2+
import { registerUpdater, Updater } from "./helper/helper";
33

44
@registerUpdater(
55
"Accept pending invitations",

back/prisma1/scripts/add-ecoorganisme-type.ts renamed to back/prisma/scripts/add-ecoorganisme-type.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { prisma } from "../../src/generated/prisma-client";
2-
import { Updater, registerUpdater } from "./helper/helper";
1+
import prisma from "src/prisma";
2+
import { registerUpdater, Updater } from "./helper/helper";
33

44
@registerUpdater(
55
"Add the eco-organisme type",
@@ -8,14 +8,16 @@ import { Updater, registerUpdater } from "./helper/helper";
88
)
99
export class AddEcoOrganismeType implements Updater {
1010
async run() {
11-
const ecoOrganismes = await prisma.ecoOrganismes();
11+
const ecoOrganismes = await prisma.ecoOrganisme.findMany();
1212
for (const ecoOrganisme of ecoOrganismes) {
13-
const company = await prisma.company({
13+
const company = await prisma.company.findOne({where: {
14+
1415
siret: ecoOrganisme.siret
16+
}
1517
});
1618

1719
if (company && !company.companyTypes.includes("ECO_ORGANISME")) {
18-
await prisma.updateCompany({
20+
await prisma.company.update({
1921
data: {
2022
companyTypes: {
2123
set: company.companyTypes.concat(["ECO_ORGANISME"])
File renamed without changes.

back/prisma1/scripts/migrate-packagings.ts renamed to back/prisma/scripts/migrate-packagings.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as readline from "readline";
2-
import { Updater, registerUpdater } from "./helper/helper";
3-
import { prisma } from "../../src/generated/prisma-client";
4-
import { Packagings } from "../../src/generated/graphql/types";
2+
import { Packagings } from "src/generated/graphql/types";
3+
import prisma from "src/prisma";
4+
import { registerUpdater, Updater } from "./helper/helper";
55

66
@registerUpdater(
77
"Migrate packagings",
@@ -16,7 +16,7 @@ export class MigratePackagingsUpdater implements Updater {
1616

1717
try {
1818
// Cannot do { where: {wasteDetailsPackagingInfos: null}} here :(
19-
const forms = await prisma.forms();
19+
const forms = await prisma.form.findMany();
2020
const notMigratedForms = forms.filter(
2121
f => f.wasteDetailsPackagingInfos == null
2222
);
@@ -27,7 +27,7 @@ export class MigratePackagingsUpdater implements Updater {
2727

2828
const updateParams = notMigratedForms.map(form => {
2929
const { wasteDetailsNumberOfPackages: numberOfPackages } = form;
30-
const packagings: Packagings[] = form.wasteDetailsPackagings ?? [];
30+
const packagings = form.wasteDetailsPackagings as Packagings[] ?? [];
3131

3232
// If numberOfPackages is 0 or less we obviously have corrupted data
3333
// So we "kind of uncorrupt" the data by assigning 0 to each
@@ -67,7 +67,7 @@ export class MigratePackagingsUpdater implements Updater {
6767
await Promise.all(
6868
updateParams
6969
.splice(0, BATCH_SIZE)
70-
.map(param => prisma.updateForm(param))
70+
.map(param => prisma.form.update(param))
7171
);
7272
counter++;
7373

back/prisma/scripts/set-contacts.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import axios from "axios";
2+
import { addContact } from "src/common/mails.helper";
3+
import prisma from "src/prisma";
4+
import { Updater, registerUpdater } from "./helper/helper";
5+
6+
type Contact = { Email: string; Name?: string };
7+
8+
@registerUpdater(
9+
"Set contacts in Mailjet",
10+
`Add every user to Mailjet, so that the newsletter are automatically sent to everyone`,
11+
false
12+
)
13+
export class SetContactsUpdater implements Updater {
14+
run() {
15+
console.info("Starting script to set contacts in mailjet...");
16+
17+
try {
18+
return prisma.user
19+
.findMany({ orderBy: { createdAt: "desc" } })
20+
.then(async users => {
21+
const latestContacts = await axios.get<Contact[]>(
22+
"http://td-mail/contact"
23+
);
24+
25+
const contactsToCreate = [];
26+
for (const user of users) {
27+
// As soon as one of the user is in the 10 latest contacts, stop picking users
28+
if (
29+
latestContacts.data.find(e => e.Email === user.email) !==
30+
undefined
31+
) {
32+
break;
33+
}
34+
contactsToCreate.push(user);
35+
}
36+
37+
return Promise.all(
38+
contactsToCreate.map(c =>
39+
addContact({ email: c.email, name: c.name }).catch(err =>
40+
console.error(`Error for email ${c.email}`, err)
41+
)
42+
)
43+
);
44+
});
45+
} catch (err) {
46+
console.error("☠ Something went wrong during the update", err);
47+
throw new Error();
48+
}
49+
}
50+
}

back/prisma1/scripts/update-eco-organismes.ts renamed to back/prisma/scripts/update-eco-organismes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Updater, registerUpdater } from "./helper/helper";
2-
import { prisma } from "../../src/generated/prisma-client";
1+
import prisma from "src/prisma";
2+
import { registerUpdater, Updater } from "./helper/helper";
33

44
const ecoOrganismes = [
55
{
@@ -112,7 +112,7 @@ const ecoOrganismes = [
112112
export class UpdateEcoOrganismesUpdater implements Updater {
113113
async run() {
114114
for (const ecoOrganisme of ecoOrganismes) {
115-
await prisma.upsertEcoOrganisme({
115+
await prisma.ecoOrganisme.upsert({
116116
create: ecoOrganisme,
117117
update: ecoOrganisme,
118118
where: {

back/prisma1/scripts/update-next-destination-country.ts renamed to back/prisma/scripts/update-next-destination-country.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Updater, registerUpdater } from "./helper/helper";
2-
import { prisma } from "../../src/generated/prisma-client";
1+
import prisma from "src/prisma";
2+
import { registerUpdater, Updater } from "./helper/helper";
33

44
@registerUpdater(
55
"Set default value of FR for next destination company country",
@@ -8,12 +8,12 @@ import { prisma } from "../../src/generated/prisma-client";
88
)
99
export class UpdateNextDestinationCountry implements Updater {
1010
async run() {
11-
await prisma.updateManyForms({
11+
await prisma.form.updateMany({
1212
data: {
1313
nextDestinationCompanyCountry: "FR"
1414
},
1515
where: {
16-
nextDestinationCompanySiret_not: null,
16+
nextDestinationCompanySiret: { not: null },
1717
nextDestinationCompanyCountry: null
1818
}
1919
});

back/prisma/seed.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
/**
5+
* To seed the DB, run `ts-node prisma/seed.ts`
6+
* It looks for a `seed.dev.ts` file and execute it.
7+
* Content should look something like:
8+
*
9+
* ```
10+
* import { PrismaClient } from "@prisma/client";
11+
* const prisma = new PrismaClient();
12+
* const main = async () => { ... }
13+
*
14+
* main()
15+
* .catch(e => console.error(e))
16+
* .finally(() => prisma.disconnect())
17+
* ```
18+
*/
19+
const seedPath = path.join(__dirname, "seed.dev.ts");
20+
21+
(() => {
22+
try {
23+
fs.accessSync(seedPath);
24+
} catch (err) {
25+
return;
26+
}
27+
28+
import(seedPath);
29+
})();

back/prisma1/database/company.prisma

-137
This file was deleted.

0 commit comments

Comments
 (0)