diff --git a/packages/client/src/graphql/employee/employee.graphql b/packages/client/src/graphql/employee/employee.graphql index 015470e8..044df875 100644 --- a/packages/client/src/graphql/employee/employee.graphql +++ b/packages/client/src/graphql/employee/employee.graphql @@ -56,13 +56,6 @@ query getEmployeeById($id: String!) { name email status - projects { - id - name - description - status - isBillable - } } } @@ -87,6 +80,7 @@ mutation EmployeeCreateInput($newEmployee: EmployeeCreateInput!) { id email name + status } } diff --git a/packages/client/src/graphql/employee/employee.ts b/packages/client/src/graphql/employee/employee.ts index 35e770c7..d962b323 100644 --- a/packages/client/src/graphql/employee/employee.ts +++ b/packages/client/src/graphql/employee/employee.ts @@ -71,17 +71,7 @@ export type GetEmployeeByIdQueryVariables = Types.Exact<{ id: Types.Scalars['String']; }>; -export type GetEmployeeByIdQuery = { - __typename?: 'Query'; - employee: { - __typename?: 'EmployeeModel'; - id: string; - name: string; - email: string; - status?: string | null; - projects: Array<{ __typename?: 'ProjectModel'; id: string; name: string; description: string; status: string; isBillable: boolean }>; - }; -}; +export type GetEmployeeByIdQuery = { __typename?: 'Query'; employee: { __typename?: 'EmployeeModel'; id: string; name: string; email: string; status?: string | null } }; export type GetRecordWithFavoriteProjectQueryVariables = Types.Exact<{ id: Types.Scalars['String']; @@ -108,7 +98,10 @@ export type EmployeeCreateInputMutationVariables = Types.Exact<{ newEmployee: Types.EmployeeCreateInput; }>; -export type EmployeeCreateInputMutation = { __typename?: 'Mutation'; addEmployee: { __typename?: 'EmployeeModel'; id: string; email: string; name: string } }; +export type EmployeeCreateInputMutation = { + __typename?: 'Mutation'; + addEmployee: { __typename?: 'EmployeeModel'; id: string; email: string; name: string; status?: string | null }; +}; export type EmployeeUpdateInputMutationVariables = Types.Exact<{ updateEmployee: Types.EmployeeUpdateInput; @@ -288,13 +281,6 @@ export const GetEmployeeByIdDocument = gql` name email status - projects { - id - name - description - status - isBillable - } } } `; @@ -379,6 +365,7 @@ export const EmployeeCreateInputDocument = gql` id email name + status } } `; diff --git a/packages/client/src/pages/Employee/components/form/EmployeeForm.tsx b/packages/client/src/pages/Employee/components/form/EmployeeForm.tsx index 84e3b9ca..790f3493 100644 --- a/packages/client/src/pages/Employee/components/form/EmployeeForm.tsx +++ b/packages/client/src/pages/Employee/components/form/EmployeeForm.tsx @@ -2,17 +2,12 @@ import * as Yup from 'yup'; import { Form, Formik } from 'formik'; import { Box, MenuItem, Typography } from '@mui/material'; import SendIcon from '@mui/icons-material/Send'; -import { - GetEmployeeByIdDocument, - GetEmployeeListDocument, - useEmployeeCreateInputMutation, - useEmployeeUpdateInputMutation, - useGetEmployeeByIdLazyQuery -} from '@graphql/employee/employee'; +import { GetEmployeeListDocument, useEmployeeCreateInputMutation, useEmployeeUpdateInputMutation, useGetEmployeeByIdLazyQuery } from '@graphql/employee/employee'; import { ObserverTextInput } from '@components/form/ObserverTextInput'; import { useParams } from 'react-router-dom'; import { FC, useEffect, useState } from 'react'; import { DefaultContainedButton } from '@components/StyledComponent'; +import { useSnackBar } from '@context/snackbar.context'; const FormValidation = Yup.object({ name: Yup.string().required('Required'), @@ -28,18 +23,17 @@ export const EmployeeForm: FC = ({ handleClose }) => { const { id } = useParams(); const [updateEmployee] = useEmployeeUpdateInputMutation(); const [addEmployee] = useEmployeeCreateInputMutation(); - const [getEmployeeById] = useGetEmployeeByIdLazyQuery(); + const { toggleSnackBar } = useSnackBar(); useEffect(() => { id && getEmployeeById({ variables: { - id: id as string - }, - nextFetchPolicy: 'cache-and-network' + id: id + } }).then((res) => { - if (res && res.data) { + if (res.data) { const { name, email, status } = res.data.employee; setInitialValue({ name, @@ -48,7 +42,7 @@ export const EmployeeForm: FC = ({ handleClose }) => { }); } }); - }, [id, open]); + }, [id]); return ( <> @@ -62,36 +56,31 @@ export const EmployeeForm: FC = ({ handleClose }) => { // if no id, create employee if (!id) { // after submitting the new employee re-fetch the employees via graphql - await addEmployee({ + const res = await addEmployee({ variables: { - newEmployee: { ...values, status: values.status.toString() } + newEmployee: { ...values } }, refetchQueries: [{ query: GetEmployeeListDocument }] }); + + res.data?.addEmployee && toggleSnackBar('Successfully created a new employee!', { variant: 'success' }); } else { - // after updating the employee, re-fetch the employees via graphql - await updateEmployee({ + const res = await updateEmployee({ variables: { - updateEmployee: { ...values, status: values.status.toString(), id: id } - }, - refetchQueries: [ - { - query: GetEmployeeByIdDocument, - variables: { - id: id as string - } - } - ] + updateEmployee: { ...values, id: id } + } }); + + res.data?.updateEmployee && toggleSnackBar('Successfully update an employee!', { variant: 'success' }); } return handleClose(); }} >
- - - + + + Inactive Active diff --git a/packages/client/src/pages/Employee/components/table/EmployeeTable.tsx b/packages/client/src/pages/Employee/components/table/EmployeeTable.tsx index f9d487ee..df9f8725 100644 --- a/packages/client/src/pages/Employee/components/table/EmployeeTable.tsx +++ b/packages/client/src/pages/Employee/components/table/EmployeeTable.tsx @@ -30,20 +30,15 @@ const dropdownData = [ ]; export const EmployeeTable: FC = ({ data }) => { - const [open, setOpen] = useState({ - add: false, - edit: false - }); + const [open, setOpen] = useState(false); const [searchText, setSearchText] = useState(''); const [filter, setFilter] = useState('Active'); const navigate = useNavigate(); - const handleClickOpen = (type: string) => { - setOpen((prevState) => ({ ...prevState, [type]: true })); - }; + const handleClickOpen = () => setOpen(true); - const handleOnClose = (type: string) => { - setOpen((prevState) => ({ ...prevState, [type]: false })); + const handleOnClose = () => { + setOpen(false); navigate(Paths.EMPLOYEE_lIST); }; @@ -88,7 +83,7 @@ export const EmployeeTable: FC = ({ data }) => { variant="outlined" onClick={() => { navigate(`${Paths.EMPLOYEE_lIST}/${row.id}`); - handleClickOpen('edit'); + handleClickOpen(); }} color="secondary" > @@ -100,19 +95,16 @@ export const EmployeeTable: FC = ({ data }) => { const ToolBar = ( <> - + All Employees - handleOnClose('add')}> - handleOnClose('add')} /> - @@ -137,20 +129,17 @@ export const EmployeeTable: FC = ({ data }) => { } }} /> - handleOnClose('edit')}> - handleOnClose('edit')} /> - {data.length === 0 && ( - - handleOnClose('add')}> - handleOnClose('add')} /> - )} + + + ); }; diff --git a/packages/server/src/employees/employees.service.ts b/packages/server/src/employees/employees.service.ts index 66897862..14bc103c 100644 --- a/packages/server/src/employees/employees.service.ts +++ b/packages/server/src/employees/employees.service.ts @@ -24,12 +24,36 @@ export class EmployeesService { return this.prisma.employee.findMany(); } + /** + * Check if an employee exists. If the employeeId is not provided, will + * return false. + * + * @param employeeId The employee id to check + * @returns True if the employee exists, false otherwise + */ + async exists(employeeId: string | undefined): Promise { + // If the employee ID is not provided, return false + if (!employeeId) return false; + + // Return true if there is an employee with the provided ID + const employeeCount = await this.prisma.employee.count({ + where: { + id: employeeId + } + }); + return employeeCount > 0; + } + /** * Get an employee by ID * * @return a matched employee */ async getEmployeeById(id: string): Promise { + const employeeExist = await this.exists(id); + + if (!employeeExist) throw new Error('Employee not found'); + return this.prisma.employee.findUnique({ where: { id: id