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 { Request, Response } from "express";
import Worker from "../db/models/Worker";
let manualId = 2;
//create new account
const createAccountController = async (req: Request, res: Response) => {
try {
......@@ -13,23 +12,21 @@ const createAccountController = async (req: Request, res: Response) => {
const { first_name, last_name, email, password, role } = req.body;
const account = await Worker.findAll({
const existedAccount = await Worker.findAll({
where: {
email: email,
},
});
if (account.length > 0) {
if (existedAccount.length > 0) {
return res
.status(409)
.json({ success: false, error: "AccountAlreadyExists" });
}
const hashedPassword = await bcrypt.hash(password, 10);
manualId++;
const newAccount = await Worker.create({
id: manualId,
email,
first_name: first_name,
last_name: last_name,
......@@ -37,7 +34,6 @@ const createAccountController = async (req: Request, res: Response) => {
role,
});
return res.status(201).send({
success: true,
account: {
......@@ -46,7 +42,7 @@ const createAccountController = async (req: Request, res: Response) => {
email: newAccount.email,
role: newAccount.role,
},
});
});
} catch {
return res.status(500).json({ success: false, error: "serverError" });
}
......
......@@ -12,7 +12,6 @@ const createInitialWorkerIfNotExists = async () => {
const hashedPassword = await bcrypt.hash(initialCoordinatorPassword, 10);
await Worker.create({
id:1,
email: initialCoordinatorEmail,
password: hashedPassword,
first_name: "coordinator_firstName",
......
import { DataTypes, Model, Optional, Sequelize } from "sequelize";
interface WorkerAttributes {
id:number;
id: string;
email: string;
first_name: string;
last_name: string;
password: string;
role: string;
}
export interface WorkerOutput extends Required<WorkerAttributes> {}
export interface WorkerInput extends Optional<WorkerAttributes, "id"> {}
class Worker extends Model<WorkerAttributes> implements WorkerAttributes {
declare id:number;
class Worker
extends Model<WorkerAttributes, WorkerInput>
implements WorkerAttributes
{
declare id: string;
declare email: string;
declare first_name: string;
declare last_name: string;
declare password: string;
declare role: string;
declare role: string;
declare readonly createdAt: Date;
declare readonly updatedAt: Date;
}
......@@ -25,9 +26,10 @@ export const initWorker = async (sequelizeConnection: Sequelize) => {
Worker.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
email: {
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