@@ -5,20 +5,21 @@ import {
5
5
NotFoundException
6
6
} from '@nestjs/common'
7
7
8
- import { and , count , desc , eq , ilike } from 'drizzle-orm'
8
+ import { and , count , desc , eq , getTableColumns , ilike } from 'drizzle-orm'
9
9
10
10
import { DrizzleAsyncProvider } from '@db/drizzle/drizzle.provider'
11
- import { employees } from '@db/drizzle/schema'
11
+ import { employees , users } from '@db/drizzle/schema'
12
12
import { DrizzleSchema } from '@db/drizzle/types'
13
13
14
- import { ERROR_CONSTANTS } from '@common/constants'
14
+ import { DEFAULT_EMPLOYEE_COLOR , ERROR_CONSTANTS } from '@common/constants'
15
15
import { StatusEnum } from '@common/enums'
16
16
import { ResponseWithPagination } from '@common/types'
17
17
import { calculatePagination , calculateQueryOffset } from '@common/utils'
18
18
19
19
import { GetEmployeesDTO } from './dto/get-employee.dto'
20
20
import { UpdateEmployeeDTO } from './dto/update-employee.dto'
21
21
import { Employee } from './entities/employee.entity'
22
+ import { GetOneEmployeeResponse } from './types'
22
23
23
24
@Injectable ( )
24
25
export class EmployeesService {
@@ -67,11 +68,19 @@ export class EmployeesService {
67
68
return { data, pagination }
68
69
}
69
70
70
- async findOne ( companyId : string , id : string ) : Promise < Employee > {
71
+ async findOne (
72
+ companyId : string ,
73
+ id : string
74
+ ) : Promise < GetOneEmployeeResponse > {
71
75
const [ employee ] = await this . db
72
- . select ( )
76
+ . select ( {
77
+ ...getTableColumns ( employees ) ,
78
+ email : users . email ,
79
+ genre : users . genre
80
+ } )
73
81
. from ( employees )
74
82
. where ( and ( eq ( employees . id , id ) , eq ( employees . companyId , companyId ) ) )
83
+ . innerJoin ( users , eq ( employees . userId , users . id ) )
75
84
. limit ( 1 )
76
85
77
86
if ( ! employee ) {
@@ -85,26 +94,67 @@ export class EmployeesService {
85
94
{ id, companyId } : { id : string ; companyId : string } ,
86
95
body : UpdateEmployeeDTO
87
96
) : 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
+ }
101
150
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
+ } )
107
157
108
- return updatedEmployee
158
+ return response
109
159
}
110
160
}
0 commit comments