Skip to content

Commit

Permalink
Migrate update scripts and delete prisma1
Browse files Browse the repository at this point in the history
  • Loading branch information
Riron committed Dec 1, 2020
1 parent dfd7f9e commit 6f7a557
Show file tree
Hide file tree
Showing 35 changed files with 107 additions and 22,807 deletions.
4 changes: 1 addition & 3 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"watch-graphql": "npm run graphql-codegen -- --watch",
"start": "node src/index.js",
"update": "node prisma/scripts/index.js",
"update:dev": "ts-node prisma/scripts/index.ts",
"update:dev": "ts-node -r tsconfig-paths/register prisma/scripts/index.ts",
"test": "jest",
"build": "tsc && copyfiles -e **/*.ts src/**/*.{graphql,pdf,png,ttf} prisma/**/*.{yml,prisma} dist/",
"lint": "eslint -c .eslintrc.js --ext .ts ./src",
Expand Down Expand Up @@ -64,7 +64,6 @@
"passport-local": "^1.0.0",
"passport-oauth2-client-password": "^0.1.2",
"pdf-lib": "^1.12.0",
"prisma-client-lib": "1.34.10",
"rate-limit-redis": "^2.0.0",
"request": "^2.88.2",
"winston": "^3.2.1",
Expand Down Expand Up @@ -119,7 +118,6 @@
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.4",
"prettier": "^2.0.5",
"prisma1": "^1.34.11",
"supertest": "^4.0.2",
"ts-jest": "26.0.0",
"ts-node": "^8.10.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { MigratePackagingsUpdater } from "../migrate-packagings";
const formsMock = jest.fn();
const updateFormMock = jest.fn();

jest.mock("../../../src/generated/prisma-client", () => ({
prisma: {
forms: jest.fn((...args) => formsMock(...args)),
updateForm: jest.fn((...args) => updateFormMock(...args))
jest.mock("src/prisma", () => ({
form: {
findMany: jest.fn((...args) => formsMock(...args)),
update: jest.fn((...args) => updateFormMock(...args))
}
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Updater, registerUpdater } from "./helper/helper";
import acceptPendingInvitations from "../../src/scripts/prisma/acceptPendingInvitations";
import acceptPendingInvitations from "src/scripts/prisma/acceptPendingInvitations";
import { registerUpdater, Updater } from "./helper/helper";

@registerUpdater(
"Accept pending invitations",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prisma } from "../../src/generated/prisma-client";
import { Updater, registerUpdater } from "./helper/helper";
import prisma from "src/prisma";
import { registerUpdater, Updater } from "./helper/helper";

@registerUpdater(
"Add the eco-organisme type",
Expand All @@ -8,14 +8,16 @@ import { Updater, registerUpdater } from "./helper/helper";
)
export class AddEcoOrganismeType implements Updater {
async run() {
const ecoOrganismes = await prisma.ecoOrganismes();
const ecoOrganismes = await prisma.ecoOrganisme.findMany();
for (const ecoOrganisme of ecoOrganismes) {
const company = await prisma.company({
const company = await prisma.company.findOne({where: {

siret: ecoOrganisme.siret
}
});

if (company && !company.companyTypes.includes("ECO_ORGANISME")) {
await prisma.updateCompany({
await prisma.company.update({
data: {
companyTypes: {
set: company.companyTypes.concat(["ECO_ORGANISME"])
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as readline from "readline";
import { Updater, registerUpdater } from "./helper/helper";
import { prisma } from "../../src/generated/prisma-client";
import { Packagings } from "../../src/generated/graphql/types";
import { Packagings } from "src/generated/graphql/types";
import prisma from "src/prisma";
import { registerUpdater, Updater } from "./helper/helper";

@registerUpdater(
"Migrate packagings",
Expand All @@ -16,7 +16,7 @@ export class MigratePackagingsUpdater implements Updater {

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

const updateParams = notMigratedForms.map(form => {
const { wasteDetailsNumberOfPackages: numberOfPackages } = form;
const packagings: Packagings[] = form.wasteDetailsPackagings ?? [];
const packagings = form.wasteDetailsPackagings as Packagings[] ?? [];

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

Expand Down
50 changes: 50 additions & 0 deletions back/prisma/scripts/set-contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from "axios";
import { addContact } from "src/common/mails.helper";
import prisma from "src/prisma";
import { Updater, registerUpdater } from "./helper/helper";

type Contact = { Email: string; Name?: string };

@registerUpdater(
"Set contacts in Mailjet",
`Add every user to Mailjet, so that the newsletter are automatically sent to everyone`,
false
)
export class SetContactsUpdater implements Updater {
run() {
console.info("Starting script to set contacts in mailjet...");

try {
return prisma.user
.findMany({ orderBy: { createdAt: "desc" } })
.then(async users => {
const latestContacts = await axios.get<Contact[]>(
"http://td-mail/contact"
);

const contactsToCreate = [];
for (const user of users) {
// As soon as one of the user is in the 10 latest contacts, stop picking users
if (
latestContacts.data.find(e => e.Email === user.email) !==
undefined
) {
break;
}
contactsToCreate.push(user);
}

return Promise.all(
contactsToCreate.map(c =>
addContact({ email: c.email, name: c.name }).catch(err =>
console.error(`Error for email ${c.email}`, err)
)
)
);
});
} catch (err) {
console.error("☠ Something went wrong during the update", err);
throw new Error();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Updater, registerUpdater } from "./helper/helper";
import { prisma } from "../../src/generated/prisma-client";
import prisma from "src/prisma";
import { registerUpdater, Updater } from "./helper/helper";

const ecoOrganismes = [
{
Expand Down Expand Up @@ -112,7 +112,7 @@ const ecoOrganismes = [
export class UpdateEcoOrganismesUpdater implements Updater {
async run() {
for (const ecoOrganisme of ecoOrganismes) {
await prisma.upsertEcoOrganisme({
await prisma.ecoOrganisme.upsert({
create: ecoOrganisme,
update: ecoOrganisme,
where: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Updater, registerUpdater } from "./helper/helper";
import { prisma } from "../../src/generated/prisma-client";
import prisma from "src/prisma";
import { registerUpdater, Updater } from "./helper/helper";

@registerUpdater(
"Set default value of FR for next destination company country",
Expand All @@ -8,12 +8,12 @@ import { prisma } from "../../src/generated/prisma-client";
)
export class UpdateNextDestinationCountry implements Updater {
async run() {
await prisma.updateManyForms({
await prisma.form.updateMany({
data: {
nextDestinationCompanyCountry: "FR"
},
where: {
nextDestinationCompanySiret_not: null,
nextDestinationCompanySiret: { not: null },
nextDestinationCompanyCountry: null
}
});
Expand Down
29 changes: 29 additions & 0 deletions back/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";

/**
* To seed the DB, run `ts-node prisma/seed.ts`
* It looks for a `seed.dev.ts` file and execute it.
* Content should look something like:
*
* ```
* import { PrismaClient } from "@prisma/client";
* const prisma = new PrismaClient();
* const main = async () => { ... }
*
* main()
* .catch(e => console.error(e))
* .finally(() => prisma.disconnect())
* ```
*/
const seedPath = path.join(__dirname, "seed.dev.ts");

(() => {
try {
fs.accessSync(seedPath);
} catch (err) {
return;
}

import(seedPath);
})();
137 changes: 0 additions & 137 deletions back/prisma1/database/company.prisma

This file was deleted.

Loading

0 comments on commit 6f7a557

Please sign in to comment.