Sfoglia il codice sorgente

refactor: reorder/rename some users model properties, add some missing properties

Kristian Vos 2 mesi fa
parent
commit
0ca85d6734

+ 25 - 16
backend/src/modules/DataModule/migrations/1725485641-create-users-table.ts

@@ -12,10 +12,6 @@ export const up = async ({
 			autoNull: false,
 			primaryKey: true
 		},
-		name: {
-			type: DataTypes.STRING,
-			allowNull: false
-		},
 		username: {
 			type: DataTypes.STRING,
 			allowNull: false
@@ -24,22 +20,27 @@ export const up = async ({
 			type: DataTypes.ENUM("admin", "moderator", "user"),
 			allowNull: false
 		},
-		emailAddress: {
-			type: DataTypes.STRING,
-			allowNull: false
-		},
-		emailVerifiedAt: {
-			type: DataTypes.DATE,
-			allowNull: true
+		emailVerified: {
+			type: DataTypes.BOOLEAN,
+			allowNull: true,
+			defaultValue: false
 		},
 		emailVerificationToken: {
 			type: DataTypes.STRING,
 			allowNull: true
 		},
+		emailAddress: {
+			type: DataTypes.STRING,
+			allowNull: false
+		},
 		avatarType: {
 			type: DataTypes.ENUM("gravatar", "initials"),
 			allowNull: false
 		},
+		avatarUrl: {
+			type: DataTypes.STRING,
+			allowNull: true
+		},
 		avatarColor: {
 			type: DataTypes.ENUM(
 				"blue",
@@ -51,15 +52,11 @@ export const up = async ({
 			),
 			allowNull: true
 		},
-		avatarUrl: {
-			type: DataTypes.STRING,
-			allowNull: true
-		},
 		password: {
 			type: DataTypes.STRING,
 			allowNull: false
 		},
-		passwordResetToken: {
+		passwordResetCode: {
 			type: DataTypes.STRING,
 			allowNull: true
 		},
@@ -67,6 +64,14 @@ export const up = async ({
 			type: DataTypes.DATE,
 			allowNull: true
 		},
+		passwordSetCode: {
+			type: DataTypes.STRING,
+			allowNull: true
+		},
+		passwordSetExpiresAt: {
+			type: DataTypes.DATE,
+			allowNull: true
+		},
 		githubId: {
 			type: DataTypes.BIGINT,
 			allowNull: true
@@ -80,6 +85,10 @@ export const up = async ({
 			allowNull: false,
 			defaultValue: 0
 		},
+		name: {
+			type: DataTypes.STRING,
+			allowNull: false
+		},
 		location: {
 			type: DataTypes.STRING,
 			allowNull: true

+ 44 - 24
backend/src/modules/DataModule/models/User.ts

@@ -32,35 +32,41 @@ export class User extends Model<
 > {
 	declare _id: CreationOptional<ObjectIdType>;
 
-	declare name: string;
-
 	declare username: string;
 
 	declare role: UserRole;
 
-	declare emailAddress: string;
-
-	declare emailVerifiedAt: CreationOptional<Date | null>;
+	declare emailVerified: CreationOptional<boolean>;
 
 	declare emailVerificationToken: CreationOptional<string | null>;
 
-	declare avatarType: UserAvatarType;
+	declare emailAddress: string;
 
-	declare avatarColor: CreationOptional<UserAvatarColor | null>;
+	declare avatarType: UserAvatarType;
 
 	declare avatarUrl: CreationOptional<string | null>;
 
+	declare avatarColor: CreationOptional<UserAvatarColor | null>;
+
+	// Services
 	declare password: string;
 
-	declare passwordResetToken: CreationOptional<string | null>;
+	declare passwordResetCode: CreationOptional<string | null>;
 
 	declare passwordResetExpiresAt: CreationOptional<Date | null>;
 
+	declare passwordSetCode: CreationOptional<string | null>;
+
+	declare passwordSetExpiresAt: CreationOptional<Date | null>;
+
 	declare githubId: CreationOptional<number | null>;
 
 	declare githubAccessToken: CreationOptional<string | null>;
+	// End services
 
+	// Statistics
 	declare songsRequested: CreationOptional<number>;
+	// End statistics
 
 	// declare likedSongsPlaylist: Types.ObjectId;
 
@@ -68,10 +74,13 @@ export class User extends Model<
 
 	// declare favoriteStations: Types.ObjectId[];
 
+	declare name: string;
+
 	declare location: CreationOptional<string | null>;
 
 	declare bio: CreationOptional<string | null>;
 
+	// Preferences
 	// declare orderOfPlaylists: Types.ObjectId[];
 
 	declare nightmode: CreationOptional<boolean>;
@@ -83,6 +92,7 @@ export class User extends Model<
 	declare anonymousSongRequests: CreationOptional<boolean>;
 
 	declare activityWatch: CreationOptional<boolean>;
+	// End preferences
 
 	declare createdAt: CreationOptional<Date>;
 
@@ -152,16 +162,13 @@ export class User extends Model<
 	};
 }
 
+// TODO add validation
 export const schema = {
 	_id: {
 		type: DataTypes.OBJECTID,
 		autoNull: false,
 		primaryKey: true
 	},
-	name: {
-		type: DataTypes.STRING,
-		allowNull: false
-	},
 	username: {
 		type: DataTypes.STRING,
 		allowNull: false
@@ -170,35 +177,36 @@ export const schema = {
 		type: DataTypes.ENUM(...Object.values(UserRole)),
 		allowNull: false
 	},
-	emailAddress: {
-		type: DataTypes.STRING,
-		allowNull: false
-	},
-	emailVerifiedAt: {
-		type: DataTypes.DATE,
-		allowNull: true
+	emailVerified: {
+		type: DataTypes.BOOLEAN,
+		allowNull: true,
+		defaultValue: false
 	},
 	emailVerificationToken: {
 		type: DataTypes.STRING,
 		allowNull: true
 	},
+	emailAddress: {
+		type: DataTypes.STRING,
+		allowNull: false
+	},
 	avatarType: {
 		type: DataTypes.ENUM(...Object.values(UserAvatarType)),
 		allowNull: false
 	},
-	avatarColor: {
-		type: DataTypes.ENUM(...Object.values(UserAvatarColor)),
-		allowNull: true
-	},
 	avatarUrl: {
 		type: DataTypes.STRING,
 		allowNull: true
 	},
+	avatarColor: {
+		type: DataTypes.ENUM(...Object.values(UserAvatarColor)),
+		allowNull: true
+	},
 	password: {
 		type: DataTypes.STRING,
 		allowNull: false
 	},
-	passwordResetToken: {
+	passwordResetCode: {
 		type: DataTypes.STRING,
 		allowNull: true
 	},
@@ -206,6 +214,14 @@ export const schema = {
 		type: DataTypes.DATE,
 		allowNull: true
 	},
+	passwordSetCode: {
+		type: DataTypes.STRING,
+		allowNull: true
+	},
+	passwordSetExpiresAt: {
+		type: DataTypes.DATE,
+		allowNull: true
+	},
 	githubId: {
 		type: DataTypes.BIGINT,
 		allowNull: true
@@ -219,6 +235,10 @@ export const schema = {
 		allowNull: false,
 		defaultValue: 0
 	},
+	name: {
+		type: DataTypes.STRING,
+		allowNull: false
+	},
 	location: {
 		type: DataTypes.STRING,
 		allowNull: true