Skip to content

Commit e699c76

Browse files
authored
Merge pull request #107 from MatheusAFD/fix/106
Fix/106
2 parents 1559c82 + 07ad355 commit e699c76

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

src/modules/employees/employees.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { CreateEmployeeDTO, UpdateEmployeeDTO } from './dto'
2929
import { GetEmployeesDTO } from './dto/get-employee.dto'
3030
import { EmployeesService } from './employees.service'
3131
import { Employee } from './entities/employee.entity'
32+
import { GetOneEmployeeResponse } from './types'
3233
import { CreateEmployeeAndUserUseCase } from './use-cases'
3334

3435
@Controller('employees')
@@ -76,7 +77,7 @@ export class EmployeesController {
7677
findOne(
7778
@Param('id') id: string,
7879
@CurrentUser() user: AuthUser
79-
): Promise<Employee> {
80+
): Promise<GetOneEmployeeResponse> {
8081
return this.employeesService.findOne(user.companyId, id)
8182
}
8283

src/modules/employees/employees.service.ts

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ import {
55
NotFoundException
66
} from '@nestjs/common'
77

8-
import { and, count, desc, eq, ilike } from 'drizzle-orm'
8+
import { and, count, desc, eq, getTableColumns, ilike } from 'drizzle-orm'
99

1010
import { DrizzleAsyncProvider } from '@db/drizzle/drizzle.provider'
11-
import { employees } from '@db/drizzle/schema'
11+
import { employees, users } from '@db/drizzle/schema'
1212
import { DrizzleSchema } from '@db/drizzle/types'
1313

14-
import { ERROR_CONSTANTS } from '@common/constants'
14+
import { DEFAULT_EMPLOYEE_COLOR, ERROR_CONSTANTS } from '@common/constants'
1515
import { StatusEnum } from '@common/enums'
1616
import { ResponseWithPagination } from '@common/types'
1717
import { calculatePagination, calculateQueryOffset } from '@common/utils'
1818

1919
import { GetEmployeesDTO } from './dto/get-employee.dto'
2020
import { UpdateEmployeeDTO } from './dto/update-employee.dto'
2121
import { Employee } from './entities/employee.entity'
22+
import { GetOneEmployeeResponse } from './types'
2223

2324
@Injectable()
2425
export class EmployeesService {
@@ -67,11 +68,19 @@ export class EmployeesService {
6768
return { data, pagination }
6869
}
6970

70-
async findOne(companyId: string, id: string): Promise<Employee> {
71+
async findOne(
72+
companyId: string,
73+
id: string
74+
): Promise<GetOneEmployeeResponse> {
7175
const [employee] = await this.db
72-
.select()
76+
.select({
77+
...getTableColumns(employees),
78+
email: users.email,
79+
genre: users.genre
80+
})
7381
.from(employees)
7482
.where(and(eq(employees.id, id), eq(employees.companyId, companyId)))
83+
.innerJoin(users, eq(employees.userId, users.id))
7584
.limit(1)
7685

7786
if (!employee) {
@@ -85,26 +94,67 @@ export class EmployeesService {
8594
{ id, companyId }: { id: string; companyId: string },
8695
body: UpdateEmployeeDTO
8796
): Promise<Employee> {
88-
const { name, status, color } = body
89-
90-
const employee = await this.findOne(companyId, id)
91-
92-
const [updatedEmployee] = await this.db
93-
.update(employees)
94-
.set({
95-
name: name ?? employee.name,
96-
color: color ?? employee.color,
97-
status: status ?? employee.status
98-
})
99-
.where(and(eq(employees.id, id), eq(employees.companyId, companyId)))
100-
.returning()
97+
const {
98+
name,
99+
email,
100+
phone,
101+
isWhatsapp = false,
102+
status = StatusEnum.ACTIVE,
103+
color = DEFAULT_EMPLOYEE_COLOR,
104+
birthdate,
105+
genre,
106+
address,
107+
neighborhood,
108+
city,
109+
state,
110+
zipcode,
111+
number
112+
} = body
113+
114+
const response = await this.db.transaction(async (trx) => {
115+
const employee = await this.findOne(companyId, id)
116+
117+
const [updatedEmployee] = await trx
118+
.update(employees)
119+
.set({
120+
name: name ?? employee.name,
121+
color: color ?? employee.color,
122+
status: status ?? employee.status,
123+
address: address ?? employee.address,
124+
neighborhood: neighborhood ?? employee.neighborhood,
125+
city: city ?? employee.city,
126+
state: state ?? employee.state,
127+
zipcode: zipcode ?? employee.zipcode,
128+
number: number ?? employee.number,
129+
phone: phone ?? employee.phone,
130+
isWhatsapp: isWhatsapp ?? employee.isWhatsapp,
131+
birthdate: birthdate ?? employee.birthdate
132+
})
133+
.where(and(eq(employees.id, id), eq(employees.companyId, companyId)))
134+
.returning()
135+
136+
const [updatedUser] = await trx
137+
.update(users)
138+
.set({
139+
email: email ?? employee.email,
140+
genre: genre ?? employee.genre
141+
})
142+
.where(eq(users.id, employee.userId))
143+
.returning()
144+
145+
if (!updatedEmployee) {
146+
throw new InternalServerErrorException(
147+
ERROR_CONSTANTS.EMPLOYEE.UPDATE_FAILED
148+
)
149+
}
101150

102-
if (!updatedEmployee) {
103-
throw new InternalServerErrorException(
104-
ERROR_CONSTANTS.EMPLOYEE.UPDATE_FAILED
105-
)
106-
}
151+
return {
152+
...updatedEmployee,
153+
genre: updatedUser.genre,
154+
email: updatedUser.email
155+
}
156+
})
107157

108-
return updatedEmployee
158+
return response
109159
}
110160
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Employee } from '../entities/employee.entity'
2+
3+
export interface GetOneEmployeeResponse extends Employee {
4+
email: string
5+
genre: string | null
6+
}

src/modules/employees/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './get-one-employee-response'

0 commit comments

Comments
 (0)