Skip to content
Snippets Groups Projects
Commit 4f112037 authored by alrwasheda's avatar alrwasheda :speech_balloon:
Browse files

UUIDV4 Id for Worker-table

parent 36b81a9a
No related branches found
No related tags found
No related merge requests found
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import { Request, Response } from "express"; import { Request, Response } from "express";
import Worker from "../db/models/Worker"; import Worker from "../db/models/Worker";
let manualId = 2;
//create new account //create new account
const createAccountController = async (req: Request, res: Response) => { const createAccountController = async (req: Request, res: Response) => {
try { try {
...@@ -13,23 +12,21 @@ const createAccountController = async (req: Request, res: Response) => { ...@@ -13,23 +12,21 @@ const createAccountController = async (req: Request, res: Response) => {
const { first_name, last_name, email, password, role } = req.body; const { first_name, last_name, email, password, role } = req.body;
const account = await Worker.findAll({ const existedAccount = await Worker.findAll({
where: { where: {
email: email, email: email,
}, },
}); });
if (account.length > 0) { if (existedAccount.length > 0) {
return res return res
.status(409) .status(409)
.json({ success: false, error: "AccountAlreadyExists" }); .json({ success: false, error: "AccountAlreadyExists" });
} }
const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10);
manualId++;
const newAccount = await Worker.create({ const newAccount = await Worker.create({
id: manualId,
email, email,
first_name: first_name, first_name: first_name,
last_name: last_name, last_name: last_name,
...@@ -37,7 +34,6 @@ const createAccountController = async (req: Request, res: Response) => { ...@@ -37,7 +34,6 @@ const createAccountController = async (req: Request, res: Response) => {
role, role,
}); });
return res.status(201).send({ return res.status(201).send({
success: true, success: true,
account: { account: {
...@@ -46,7 +42,7 @@ const createAccountController = async (req: Request, res: Response) => { ...@@ -46,7 +42,7 @@ const createAccountController = async (req: Request, res: Response) => {
email: newAccount.email, email: newAccount.email,
role: newAccount.role, role: newAccount.role,
}, },
}); });
} catch { } catch {
return res.status(500).json({ success: false, error: "serverError" }); return res.status(500).json({ success: false, error: "serverError" });
} }
......
...@@ -12,7 +12,6 @@ const createInitialWorkerIfNotExists = async () => { ...@@ -12,7 +12,6 @@ const createInitialWorkerIfNotExists = async () => {
const hashedPassword = await bcrypt.hash(initialCoordinatorPassword, 10); const hashedPassword = await bcrypt.hash(initialCoordinatorPassword, 10);
await Worker.create({ await Worker.create({
id:1,
email: initialCoordinatorEmail, email: initialCoordinatorEmail,
password: hashedPassword, password: hashedPassword,
first_name: "coordinator_firstName", first_name: "coordinator_firstName",
......
import { DataTypes, Model, Optional, Sequelize } from "sequelize"; import { DataTypes, Model, Optional, Sequelize } from "sequelize";
interface WorkerAttributes { interface WorkerAttributes {
id:number; id: string;
email: string; email: string;
first_name: string; first_name: string;
last_name: string; last_name: string;
password: string; password: string;
role: string; role: string;
} }
export interface WorkerOutput extends Required<WorkerAttributes> {}
export interface WorkerInput extends Optional<WorkerAttributes, "id"> {} export interface WorkerInput extends Optional<WorkerAttributes, "id"> {}
class Worker extends Model<WorkerAttributes> implements WorkerAttributes { class Worker
declare id:number; extends Model<WorkerAttributes, WorkerInput>
implements WorkerAttributes
{
declare id: string;
declare email: string; declare email: string;
declare first_name: string; declare first_name: string;
declare last_name: string; declare last_name: string;
declare password: string; declare password: string;
declare role: string; declare role: string;
declare readonly createdAt: Date; declare readonly createdAt: Date;
declare readonly updatedAt: Date; declare readonly updatedAt: Date;
} }
...@@ -25,9 +26,10 @@ export const initWorker = async (sequelizeConnection: Sequelize) => { ...@@ -25,9 +26,10 @@ export const initWorker = async (sequelizeConnection: Sequelize) => {
Worker.init( Worker.init(
{ {
id: { id: {
type: DataTypes.INTEGER.UNSIGNED, type: DataTypes.UUID,
autoIncrement: true, defaultValue: DataTypes.UUIDV4,
primaryKey: true, primaryKey: true,
allowNull: false,
}, },
email: { email: {
type: new DataTypes.STRING(), type: new DataTypes.STRING(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment