Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: admin can add hr #546

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions prisma/migrations/20241023182559_add_hr/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Warnings:

- A unique constraint covering the columns `[companyId]` on the table `User` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateEnum
CREATE TYPE "ProjectStack" AS ENUM ('GO', 'PYTHON', 'MERN', 'NEXTJS', 'AI_GPT_APIS', 'SPRINGBOOT', 'OTHERS');

-- DropForeignKey
ALTER TABLE "Experience" DROP CONSTRAINT "Experience_userId_fkey";

-- DropForeignKey
ALTER TABLE "Project" DROP CONSTRAINT "Project_userId_fkey";

-- AlterTable
ALTER TABLE "Project" ADD COLUMN "projectThumbnail" TEXT,
ADD COLUMN "stack" "ProjectStack" NOT NULL DEFAULT 'OTHERS';

-- AlterTable
ALTER TABLE "User" ADD COLUMN "companyId" TEXT;

-- CreateTable
CREATE TABLE "Company" (
"id" TEXT NOT NULL,
"compangName" TEXT NOT NULL,
"companyLogo" TEXT,
"companyBio" TEXT NOT NULL,

CONSTRAINT "Company_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_companyId_key" ON "User"("companyId");

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Experience" ADD CONSTRAINT "Experience_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
10 changes: 10 additions & 0 deletions prisma/migrations/20241023184042_spell_error/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:

- You are about to drop the column `compangName` on the `Company` table. All the data in the column will be lost.
- Added the required column `companyName` to the `Company` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "Company" DROP COLUMN "compangName",
ADD COLUMN "companyName" TEXT NOT NULL;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ model Project {
isFeature Boolean @default(false)
}


enum ProjectStack {
GO
PYTHON
Expand Down
78 changes: 78 additions & 0 deletions src/actions/hr.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use server';

import prisma from '@/config/prisma.config';
import { ErrorHandler } from '@/lib/error';
import { withSession } from '@/lib/session';
import { SuccessResponse } from '@/lib/success';
import { HRPostSchema, HRPostSchemaType } from '@/lib/validators/hr.validator';
import { ServerActionReturnType } from '@/types/api.types';
import bcryptjs from 'bcryptjs';
import { PASSWORD_HASH_SALT_ROUNDS } from '@/config/auth.config';
import { generateRandomPassword } from '@/lib/randomPassword';

type HRReturnType = {
password: string;
userId: string;
};

export const createHR = withSession<
HRPostSchemaType,
ServerActionReturnType<HRReturnType>
>(async (session, data) => {
if (!session || !session?.user?.id || session.user.role !== 'ADMIN') {
throw new ErrorHandler('Not Authrised', 'UNAUTHORIZED');
}

const result = HRPostSchema.safeParse(data);
if (result.error) {
throw new ErrorHandler('Invalid Data', 'BAD_REQUEST');
}

const { companyBio, companyLogo, companyName, email, name } = result.data;

const userExist = await prisma.user.findFirst({
where: { email: email },
});

if (userExist)
throw new ErrorHandler('User with this email already exist', 'BAD_REQUEST');
const password = generateRandomPassword();
const hashedPassword = await bcryptjs.hash(
password,
PASSWORD_HASH_SALT_ROUNDS
);
const { user } = await prisma.$transaction(async () => {
const company = await prisma.company.create({
data: {
companyName: companyName,
companyBio: companyBio,
companyLogo: companyLogo,
companyEmail: email,
},
});

const user = await prisma.user.create({
data: {
email: email,
password: hashedPassword,
name: name,
role: 'HR',
companyId: company.id,
emailVerified: new Date(),
},
});

return { user };
});

const returnObj = {
password,
userId: user.id,
};

return new SuccessResponse(
'HR created successfully.',
201,
returnObj
).serialize();
});
16 changes: 16 additions & 0 deletions src/app/admin/add-hr/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import AddHRForm from '@/components/AddHRForm';
import React from 'react';

const page = () => {
return (
<div className="mt-10 flex flex-col items-center">
<div>
<h1 className="text-center text-4xl font-semibold">Add HR</h1>
</div>

<AddHRForm />
</div>
);
};

export default page;
Loading
Loading