Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

El names migration #442

Merged
merged 6 commits into from Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/server/migration/1606609022417-AddFirstLastName.ts
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddFirstLastName1606609022417 implements MigrationInterface {
name = 'AddFirstLastName1606609022417';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "user_model" ADD "firstName" text`);
await queryRunner.query(`ALTER TABLE "user_model" ADD "lastName" text`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "user_model" DROP COLUMN "lastName"`);
await queryRunner.query(`ALTER TABLE "user_model" DROP COLUMN "firstName"`);
}
}
4 changes: 2 additions & 2 deletions packages/server/src/backfill/backfill-phone-notifs.command.ts
@@ -1,9 +1,9 @@
import { Command } from 'nestjs-command';
import { Injectable } from '@nestjs/common';
import { Command } from 'nestjs-command';
import { PhoneNotifModel } from 'notification/phone-notif.entity';
import { IsNull } from 'typeorm';
import { TwilioService } from 'notification/twilio/twilio.service';
import { UserModel } from 'profile/user.entity';
import { IsNull } from 'typeorm';

@Injectable()
export class BackfillPhoneNotifs {
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/backfill/backfill.module.ts
Expand Up @@ -2,9 +2,14 @@ import { Module } from '@nestjs/common';
import { NotificationModule } from 'notification/notification.module';
import { BackfillPhoneNotifs } from './backfill-phone-notifs.command';
import { BackfillQuestionFirstHelpedAt } from './question-first-helped-at.command';
import { BackfillSeparateFirstLastNames } from './separate-first-last-names.command';

@Module({
imports: [NotificationModule],
providers: [BackfillPhoneNotifs, BackfillQuestionFirstHelpedAt],
providers: [
BackfillPhoneNotifs,
BackfillQuestionFirstHelpedAt,
BackfillSeparateFirstLastNames,
],
})
export class BackfillModule {}
29 changes: 29 additions & 0 deletions packages/server/src/backfill/separate-first-last-names.command.ts
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { Command } from 'nestjs-command';
import { UserModel } from 'profile/user.entity';

@Injectable()
export class BackfillSeparateFirstLastNames {
@Command({
command: 'backfill:first-last-names',
describe: 'change all names to first and last names',
autoExit: true,
})
async fix(): Promise<void> {
const users = await UserModel.find();
users.forEach((user) => {
try {
user.firstName = user.name.split(' ')[0];
user.lastName = user.name.split(' ').slice(1).join(' ');
} catch (e) {
user.firstName = user.name;
console.log(`Updating name failed for ${user.name}`);
}
});

await UserModel.save(users);
const count = UserModel.count();

console.log(`Updated names for ${count} users`);
}
}
2 changes: 2 additions & 0 deletions packages/server/src/login/login.controller.ts
Expand Up @@ -69,6 +69,8 @@ export class LoginController {
// Q: Do we need this if it's not going to change?
user = Object.assign(user, {
email: body.email,
firstName: body.first_name,
lastName: body.last_name,
name: body.first_name + ' ' + body.last_name,
photoURL: '',
});
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/profile/user.entity.ts
Expand Up @@ -25,6 +25,12 @@ export class UserModel extends BaseEntity {
@Column('text')
name: string;

@Column('text', { nullable: true })
firstName: string;

@Column('text', { nullable: true })
lastName: string;

@Column('text')
photoURL: string;

Expand Down
Expand Up @@ -10,7 +10,9 @@ ListQuestionsResponse {
"createdAt": 2020-11-02T12:00:00.000Z,
"creator": UserModel {
"email": "user@neu.edu",
"firstName": null,
"id": 1,
"lastName": null,
"name": "User",
"photoURL": "https://pics/user",
},
Expand Down Expand Up @@ -45,7 +47,9 @@ ListQuestionsResponse {
"creator": UserModel {
"desktopNotifsEnabled": false,
"email": "user@neu.edu",
"firstName": null,
"id": 1,
"lastName": null,
"name": "User",
"phoneNotifsEnabled": false,
"photoURL": "https://pics/user",
Expand Down
2 changes: 2 additions & 0 deletions packages/server/test/__snapshots__/course.integration.ts.snap
Expand Up @@ -26,7 +26,9 @@ Object {
"staffList": Array [
Object {
"email": "user@neu.edu",
"firstName": null,
"id": 1,
"lastName": null,
"name": "User",
"photoURL": "https://pics/user",
},
Expand Down
Expand Up @@ -6,7 +6,9 @@ Object {
"createdAt": "2020-03-01T05:00:00.000Z",
"creator": Object {
"email": "user@neu.edu",
"firstName": null,
"id": 1,
"lastName": null,
"name": "User",
"photoURL": "https://pics/user",
},
Expand Down