fix(user): persist updateUser correctly; add setup:api script and README update; seeders tweaks
This commit is contained in:
@@ -8,7 +8,7 @@ module.exports = {
|
||||
// Create User table
|
||||
await queryInterface.createTable('user', {
|
||||
id: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
@@ -202,7 +202,7 @@ module.exports = {
|
||||
allowNull: false
|
||||
},
|
||||
authorId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: 'user',
|
||||
@@ -236,7 +236,7 @@ module.exports = {
|
||||
allowNull: false
|
||||
},
|
||||
authorId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: 'user',
|
||||
@@ -306,7 +306,7 @@ module.exports = {
|
||||
// Create Favorite table (junction table for User-Article)
|
||||
await queryInterface.createTable('favorite', {
|
||||
userId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
@@ -342,7 +342,7 @@ module.exports = {
|
||||
// Create Follower table (junction table for User-User)
|
||||
await queryInterface.createTable('follower', {
|
||||
userId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
@@ -353,7 +353,7 @@ module.exports = {
|
||||
onUpdate: 'CASCADE'
|
||||
},
|
||||
followerId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
@@ -437,7 +437,7 @@ module.exports = {
|
||||
onUpdate: 'CASCADE'
|
||||
},
|
||||
ownerId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: 'user',
|
||||
@@ -471,7 +471,7 @@ module.exports = {
|
||||
// Create UserRoleXref table (junction table for User-Role)
|
||||
await queryInterface.createTable('userRoleXref', {
|
||||
userId: {
|
||||
type: Sequelize.STRING(24),
|
||||
type: Sequelize.STRING(36),
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
|
||||
@@ -11,7 +11,7 @@ const UserModel = () => {
|
||||
User.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.STRING(24),
|
||||
type: DataTypes.UUID(),
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
@@ -87,16 +87,16 @@ const UserModel = () => {
|
||||
}
|
||||
);
|
||||
|
||||
User.beforeCreate((user) => {
|
||||
/*User.beforeCreate((user) => {
|
||||
if (typeof user.username === 'undefined') {
|
||||
user.username = `${user.firstname}_${user.lastname}`;
|
||||
}
|
||||
user.setPassword(user.password);
|
||||
user.role = 'User';
|
||||
});
|
||||
});*/
|
||||
|
||||
User.prototype.validPassword = function (password) {
|
||||
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
return this.hash === hash;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
try {
|
||||
// Create roles
|
||||
await queryInterface.bulkInsert('role', [
|
||||
{
|
||||
name: 'Admin',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
},
|
||||
{
|
||||
name: 'User',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}
|
||||
], {});
|
||||
|
||||
console.log('✅ Rôles créés avec succès');
|
||||
} catch (error) {
|
||||
console.error('❌ Erreur lors de la création des rôles:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
try {
|
||||
// Delete roles
|
||||
await queryInterface.bulkDelete('role', { name: { [Sequelize.Op.in]: ['Admin', 'User'] } }, {});
|
||||
|
||||
console.log('✅ Rôles supprimés avec succès');
|
||||
} catch (error) {
|
||||
console.error('❌ Erreur lors de la suppression des rôles:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -4,7 +4,7 @@ const slug = require('slug');
|
||||
|
||||
const ANIMAL_TAGS = ['Lion', 'Eagle', 'Dolphin', 'Tiger', 'Wolf'];
|
||||
|
||||
const AUTHOR_ID = '642c4459666702637dcb5066';
|
||||
// AUTHOR_ID will be resolved at runtime to the oldest user with role 'Admin'
|
||||
|
||||
const ARTICLES = [
|
||||
{
|
||||
@@ -46,6 +46,40 @@ module.exports = {
|
||||
// Get transaction for consistency
|
||||
const transaction = await queryInterface.sequelize.transaction();
|
||||
|
||||
// Resolve AUTHOR_ID: oldest user who has role 'Admin'
|
||||
let AUTHOR_ID = null;
|
||||
try {
|
||||
const adminQuery = await queryInterface.sequelize.query(
|
||||
`SELECT u.id FROM \`user\` u
|
||||
JOIN userRoleXref ur ON ur.userId = u.id
|
||||
JOIN role r ON r.id = ur.roleId
|
||||
WHERE r.name = ?
|
||||
ORDER BY u.createdAt ASC
|
||||
LIMIT 1`,
|
||||
{ replacements: ['Admin'], transaction, raw: true }
|
||||
);
|
||||
|
||||
if (adminQuery && adminQuery[0] && adminQuery[0][0] && adminQuery[0][0].id) {
|
||||
AUTHOR_ID = adminQuery[0][0].id;
|
||||
} else {
|
||||
// Fallback: pick the oldest user in `user` table
|
||||
const fallback = await queryInterface.sequelize.query(
|
||||
`SELECT id FROM \`user\` ORDER BY createdAt ASC LIMIT 1`,
|
||||
{ transaction, raw: true }
|
||||
);
|
||||
if (fallback && fallback[0] && fallback[0][0] && fallback[0][0].id) {
|
||||
AUTHOR_ID = fallback[0][0].id;
|
||||
}
|
||||
}
|
||||
|
||||
if (!AUTHOR_ID) {
|
||||
throw new Error('Aucun utilisateur trouvé pour être utilisé comme auteur (Admin absent).');
|
||||
}
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
|
||||
try {
|
||||
// Create articles
|
||||
for (const articleData of ARTICLES) {
|
||||
@@ -127,7 +161,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
async down(queryInterface) {
|
||||
try {
|
||||
// Delete in correct order due to foreign keys
|
||||
await queryInterface.sequelize.query('DELETE FROM tagList WHERE 1=1');
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
try {
|
||||
// Create roles
|
||||
await queryInterface.bulkInsert('role', [
|
||||
{
|
||||
name: 'Admin',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
},
|
||||
{
|
||||
name: 'User',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}
|
||||
], {});
|
||||
|
||||
console.log('✅ Rôles créés avec succès');
|
||||
} catch (error) {
|
||||
console.error('❌ Erreur lors de la création des rôles:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
try {
|
||||
// Delete roles
|
||||
await queryInterface.bulkDelete('role', { name: { [Sequelize.Op.in]: ['Admin', 'User'] } }, {});
|
||||
|
||||
console.log('✅ Rôles supprimés avec succès');
|
||||
} catch (error) {
|
||||
console.error('❌ Erreur lors de la suppression des rôles:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,2 +0,0 @@
|
||||
// Seeders managed by individual migration files
|
||||
// See seeders directory for specific seeder implementations
|
||||
Reference in New Issue
Block a user