-
Notifications
You must be signed in to change notification settings - Fork 3
/
1722084520361-pos-users.ts
78 lines (70 loc) · 2.81 KB
/
1722084520361-pos-users.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
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* @module
* @hidden
*/
import { MigrationInterface, QueryRunner, TableColumn, TableForeignKey } from 'typeorm';
import PointOfSale from '../entity/point-of-sale/point-of-sale';
import User, { TermsOfServiceStatus, UserType } from '../entity/user/user';
export class PosUsers1722084520361 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn('point_of_sale', new TableColumn({
name: 'userId',
type: 'integer',
isNullable: true, // Temporary for migration
isUnique: true,
}));
await queryRunner.createForeignKey('point_of_sale', new TableForeignKey({
columnNames: ['userId'],
referencedColumnNames: ['id'],
referencedTableName: 'user',
onDelete: 'RESTRICT',
}));
const pointsOfSale = await queryRunner.manager.getRepository(PointOfSale).find({ withDeleted: true, relations: { user: true } });
await Promise.all(pointsOfSale.map(async (pointOfSale): Promise<User> => {
const user = await queryRunner.manager.getRepository(User)
.save({
firstName: `Point of Sale ${pointOfSale.id}`,
type: UserType.POINT_OF_SALE,
active: true,
acceptedToS: TermsOfServiceStatus.NOT_REQUIRED,
});
pointOfSale.user = user;
await queryRunner.manager.getRepository(PointOfSale).save(pointOfSale);
return user;
}));
await queryRunner.changeColumn('point_of_sale', 'userId', new TableColumn({
name: 'userId',
type: 'integer',
isNullable: false,
isUnique: true,
}));
}
public async down(queryRunner: QueryRunner): Promise<void> {
const posTable = await queryRunner.getTable('point_of_sale');
const userForeignKey = posTable.foreignKeys.find((fk) => fk.columnNames.indexOf('userId') !== -1);
await queryRunner.dropForeignKey('point_of_sale', userForeignKey);
await queryRunner.dropColumn('point_of_sale', 'userId');
await queryRunner.manager.getRepository(User).delete({
type: UserType.POINT_OF_SALE,
});
}
}