-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DucHuy2801
committed
May 16, 2024
1 parent
64c16a4
commit b55515c
Showing
1 changed file
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,102 @@ | |
const { DataTypes } = require("sequelize") | ||
const sequelize = require("../database/connect.mysql") | ||
const User = require("./user.model") | ||
const { RoleUser } = require("../common/status") | ||
const bcrypt = require('bcrypt'); | ||
|
||
class TourGuide extends User {} | ||
TourGuide.init({}, { sequelize, modelName: 'tourguide'}) | ||
TourGuide.init({ | ||
tour_guide_id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
password: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
lastname: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
firstname: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
gender: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
dob: { | ||
type: DataTypes.DATE, | ||
allowNull: true | ||
}, | ||
avatar: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
phone_number: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
validate: { | ||
len: [10-12] | ||
} | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
unique: true | ||
}, | ||
code: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
expired_time_code: { | ||
type: DataTypes.DATE, | ||
allowNull: true | ||
}, | ||
role_user: { | ||
type: DataTypes.ENUM(RoleUser.GUIDER), | ||
allowNull: false | ||
}, | ||
access_token: { | ||
type: DataTypes.TEXT, | ||
allowNull: true | ||
}, | ||
refresh_token: { | ||
type: DataTypes.TEXT, | ||
allowNull: true | ||
} | ||
|
||
}, { sequelize, modelName: 'tour_guide'}) | ||
|
||
const createTourGuideDefault = async () => { | ||
try { | ||
const tourGuides = [ | ||
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE }, | ||
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE }, | ||
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE }, | ||
] | ||
|
||
for (const tourGuide of tourGuides) { | ||
const existTourGuide = await TourGuide.findOne({ where: { email: tourGuide.email } }); | ||
if (!existTourGuide) { | ||
const hashedPassword = await bcrypt.hash(guide.password, 10); | ||
const tourGuideInstance = new TourGuide({ | ||
email: tourGuide.email, | ||
password: hashedPassword, | ||
role_user: tourGuide.role_user, | ||
}); | ||
|
||
await tourGuideInstance.save(); | ||
} | ||
} | ||
} catch (error) { | ||
throw new Error("Error: ", error) | ||
} | ||
} | ||
|
||
createTourGuideDefault() | ||
|
||
module.exports = TourGuide | ||
|