1
+ import { AuthenticationError } from 'apollo-server' ;
2
+ import JobApplication from '../models/JobApplication'
3
+ import applicationCycleResolver from './applicationCycleResolver' ;
4
+ import { CustomGraphQLError } from '../utils/customErrorHandler' ;
5
+ import { LoggedUserModel } from '../models/AuthUser' ;
6
+ import { jobModels } from '../models/jobModels' ;
7
+
8
+ interface formData {
9
+ traineeId : string ,
10
+ jobId : string ,
11
+ essay : string ,
12
+ resume : string
13
+ }
14
+
15
+ interface getOneFormData {
16
+ applicationId : string
17
+ }
18
+
19
+ interface statusData {
20
+ applicationId : string ,
21
+ status : string
22
+ }
23
+
24
+ const formatDate = ( date :Date ) => {
25
+ const year = date . getFullYear ( ) ;
26
+ const month = ( `0${ date . getMonth ( ) + 1 } ` ) . slice ( - 2 ) ;
27
+ const day = ( `0${ date . getDate ( ) } ` ) . slice ( - 2 ) ;
28
+ return `${ year } -${ month } -${ day } ` ;
29
+ } ;
30
+
31
+ export const jobApplicationResolver = {
32
+ Query : {
33
+ getOwnJobApplications : async ( _ :any , { input } :{ input : formData } , cxt :any ) => {
34
+ if ( ! cxt . currentUser ) {
35
+ throw new AuthenticationError ( 'You must be logged in' ) ;
36
+ }
37
+
38
+ const userId = cxt . currentUser . _id
39
+
40
+ const applications = await JobApplication . find ( { userId} )
41
+ . populate ( 'jobId' )
42
+
43
+ return applications . map ( application => {
44
+ return {
45
+ _id : application . _id ,
46
+ userId : application . userId ,
47
+ essay : application . essay ,
48
+ resume : application . resume ,
49
+ status : application . status ,
50
+ jobId : application . jobId ,
51
+ createdAt : formatDate ( application . createdAt )
52
+ }
53
+ } )
54
+ } ,
55
+ getAllJobApplications : async ( _ :any , { input } :{ input : formData } , cxt :any ) => {
56
+ if ( ! cxt . currentUser ) {
57
+ throw new AuthenticationError ( 'You must be logged in' ) ;
58
+ }
59
+
60
+ const userWithRole = await LoggedUserModel . findById (
61
+ cxt . currentUser ?. _id
62
+ ) . populate ( "role" ) ;
63
+
64
+ if (
65
+ ! userWithRole ||
66
+ ( ( userWithRole . role as any ) ?. roleName !== "admin" &&
67
+ ( userWithRole . role as any ) ?. roleName !== "superAdmin" )
68
+ ) {
69
+ throw new CustomGraphQLError (
70
+ "You do not have permission to perform this action"
71
+ ) ;
72
+ }
73
+
74
+ const applications = await JobApplication . find ( ) . populate ( 'jobId' ) . populate ( 'userId' )
75
+ return applications . map ( application => {
76
+ return {
77
+ _id : application . _id ,
78
+ userId : application . userId ,
79
+ essay : application . essay ,
80
+ resume : application . resume ,
81
+ status : application . status ,
82
+ jobId : application . jobId ,
83
+ createdAt : formatDate ( application . createdAt )
84
+ }
85
+ } )
86
+ } ,
87
+ getOneJobApplication : async ( _ :any , { input } :{ input : getOneFormData } , cxt :any ) => {
88
+ if ( ! cxt . currentUser ) {
89
+ throw new AuthenticationError ( 'You must be logged in' ) ;
90
+ }
91
+
92
+ const userWithRole = await LoggedUserModel . findById (
93
+ cxt . currentUser ?. _id
94
+ ) . populate ( "role" ) ;
95
+
96
+ if (
97
+ ! userWithRole ||
98
+ ( ( userWithRole . role as any ) ?. roleName !== "admin" &&
99
+ ( userWithRole . role as any ) ?. roleName !== "superAdmin" )
100
+ ) {
101
+ throw new CustomGraphQLError (
102
+ "You do not have permission to perform this action"
103
+ ) ;
104
+ }
105
+
106
+ const application = await JobApplication . findOne ( { _id : input . applicationId } ) . populate ( 'jobId' ) . populate ( 'userId' )
107
+ if ( ! application ) {
108
+ throw new Error ( 'Application not found' ) ;
109
+ }
110
+
111
+ return {
112
+ _id : application . _id ,
113
+ userId : application . userId ,
114
+ essay : application . essay ,
115
+ resume : application . resume ,
116
+ status : application . status ,
117
+ jobId : application . jobId ,
118
+ createdAt : formatDate ( application . createdAt )
119
+ }
120
+ } ,
121
+ checkIfUserApplied : async ( _ : any , args : any , cxt : any ) => {
122
+ if ( ! cxt . currentUser ) {
123
+ throw new AuthenticationError ( 'You must be logged in' ) ;
124
+ }
125
+
126
+ const application = await JobApplication . findOne ( { userId : cxt . currentUser . _id , jobId : args . input . jobId } )
127
+ return {
128
+ status : application ? true : false
129
+ }
130
+ }
131
+ } ,
132
+ Mutation : {
133
+ createNewJobApplication : async ( _ :any , { input } :{ input : formData } , cxt :any ) => {
134
+ if ( ! cxt . currentUser ) {
135
+ throw new AuthenticationError ( 'You must be logged in' ) ;
136
+ }
137
+
138
+ const userId = cxt . currentUser . _id
139
+
140
+ const { jobId, essay, resume } = input ;
141
+
142
+ const existingApplication = await JobApplication . findOne ( { userId, jobId} )
143
+ if ( existingApplication ) {
144
+ throw new Error ( 'Application already exists' ) ;
145
+ }
146
+
147
+ const jobApplication = new JobApplication ( {
148
+ userId,
149
+ jobId,
150
+ essay,
151
+ resume,
152
+ } ) ;
153
+
154
+
155
+ const savedApplication = await jobApplication . save ( ) ;
156
+
157
+ return savedApplication ;
158
+ } ,
159
+ changeApplicationStatus : async ( _ :any , { input } :{ input : statusData } , cxt :any ) => {
160
+ if ( ! cxt . currentUser ) {
161
+ throw new AuthenticationError ( 'You must be logged in' ) ;
162
+ }
163
+
164
+ const userWithRole = await LoggedUserModel . findById (
165
+ cxt . currentUser ?. _id
166
+ ) . populate ( "role" ) ;
167
+
168
+ if (
169
+ ! userWithRole ||
170
+ ( ( userWithRole . role as any ) ?. roleName !== "admin" &&
171
+ ( userWithRole . role as any ) ?. roleName !== "superAdmin" )
172
+ ) {
173
+ throw new CustomGraphQLError (
174
+ "You do not have permission to perform this action"
175
+ ) ;
176
+ }
177
+
178
+ const { status, applicationId } = input
179
+ await JobApplication . findByIdAndUpdate ( applicationId , { status} )
180
+ const application = await JobApplication . findOne ( { _id :applicationId } ) . populate ( 'userId' ) . populate ( 'jobId' )
181
+ return application
182
+ }
183
+ } ,
184
+ } ;
0 commit comments