From 0045fcbcdfbef04daca54ef212965a4f87f49619 Mon Sep 17 00:00:00 2001 From: David Leek Date: Thu, 25 Jan 2024 11:19:39 +0100 Subject: [PATCH] chore: replace systemUser -1 with new -1337 system user (#5999) ## About the changes This PR replaces the old systemUser -1 in user-service.ts with the new SYSTEM_USER -1337 and adds a migration to move events created_by = -1 to -1337 ## Discussion points Does it make sense to do both of these things? Or should we skip the migration? How would this behave in a large system with hundreds of thousands of events, should this be split up? --- src/lib/services/user-service.ts | 9 ++++++--- .../20240125100000-events-system-user-old2new.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/migrations/20240125100000-events-system-user-old2new.js diff --git a/src/lib/services/user-service.ts b/src/lib/services/user-service.ts index 60d83cca8b..323439626e 100644 --- a/src/lib/services/user-service.ts +++ b/src/lib/services/user-service.ts @@ -33,7 +33,7 @@ import { TokenUserSchema } from '../openapi/spec/token-user-schema'; import PasswordMismatch from '../error/password-mismatch'; import EventService from '../features/events/event-service'; -const systemUser = new User({ id: -1, username: 'system' }); +import { SYSTEM_USER } from '../types'; export interface ICreateUser { name?: string; @@ -244,7 +244,7 @@ class UserService { return userCreated; } - private getCreatedBy(updatedBy: IUser = systemUser) { + private getCreatedBy(updatedBy: IUser = new User(SYSTEM_USER)): string { return updatedBy.username || updatedBy.email; } @@ -444,7 +444,10 @@ class UserService { async createResetPasswordEmail( receiverEmail: string, - user: IUser = systemUser, + user: IUser = new User({ + id: SYSTEM_USER.id, + username: SYSTEM_USER.username, + }), ): Promise { const receiver = await this.getByEmail(receiverEmail); if (!receiver) { diff --git a/src/migrations/20240125100000-events-system-user-old2new.js b/src/migrations/20240125100000-events-system-user-old2new.js new file mode 100644 index 0000000000..e1e69e3d77 --- /dev/null +++ b/src/migrations/20240125100000-events-system-user-old2new.js @@ -0,0 +1,14 @@ +'use strict'; + +exports.up = function (db, callback) { + db.runSql( + ` + UPDATE events SET created_by_user_id = -1337 WHERE created_by_user_id = -1; + `, + callback, + ); +}; + +exports.down = function (db, callback) { + callback(); +};