summaryrefslogtreecommitdiff
path: root/lisp/service/resume-service.lisp
blob: dca609cd6d039d79ca6bb8be4bd873baf3ce8688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(declaim (optimize (speed 0) (safety 3) (debug 3)))

(in-package :resume)

;; shared

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun state-options (states)
    (mapcar (lambda (state)
              `(:label ,(format nil "~a ~a" (abbr state) (state state)) :value ,(id state)))
            states))

  (defun visastatus-options (visastatuses)
    (mapcar (lambda (visastatus)
              `(:label ,(status visastatus) :value ,(id visastatus)))
            visastatuses))

  (defun language-options ()
    (mapcar (lambda (level)
              `(:label ,level :value ,level))
            '("A1" "A2" "B1" "B2" "C1" "C2")))

  (defun skill-checkboxes (resume-pkg user-id job-id)
    (let ((skills (get-records resume-pkg (make-instance 'skill :user_id user-id) "skill asc"))
          (job-skills (get-records resume-pkg (make-instance 'job-skill :job_id job-id) "skill_id asc")))
      (mapcar (lambda (skill)
                (let ((checked (when (intersection `(,(id skill))
                                                   (mapcar (lambda (x)
                                                             (skill_id x))
                                                           job-skills)
                                                   :test '=)
                                 '(:checked "checked" :value "on"))))
                  `(:name ,(format nil "chk_job_skill_link_~a" (id skill)) :label ,(skill skill) :field-type "checkbox" ,@checked)))
              skills)))

  (defun link-job (id skill-ids)
    (with-resume-database
      (let* ((resume-pkg (make-instance 'resume-pkg))
             (skill-id-list (cl-ppcre:split "\\|" skill-ids)))
        (loop for skill-id in skill-id-list
              do (let* ((job-skill (make-instance 'job-skill
                                                  :job_id id
                                                  :skill_id skill-id)))
                   (when (null (get-record resume-pkg job-skill))
                     (insert-record resume-pkg job-skill))))
        (delete-records-where resume-pkg
                              (make-instance 'job-skill)
                              (format nil
                                      "job_id = ~a~a"
                                      id
                                      (if (> (length skill-id-list) 0)
                                          (format nil
                                                  " and skill_id not in (~a)"
                                                  (org-ckons-core::reduce-to-comma-separated-string skill-id-list))
                                          ""))))))

  (defun link-resume ()
    )
  
  (defclass resume/resume-service (auth-service)
    ((title :initarg :title
            :initform nil
            :accessor title))
    (:documentation ""))

  (defclass resume/resume-component/view-service (resume/resume-service)
    ((components :initarg components
                 :initform nil
                 :accessor components)
     (location-p :initform nil))
    (:documentation ""))

  (defclass resume/resume-component/modify-service (resume/resume-component/view-service)
    ((form :initarg :form
           :initform nil
           :accessor form))
    (:documentation "")))

;; macros

(defmacro define-resume-component ((&key component-s rest-args link-rest-args add-fields modify-fields link-fields order-by view-p))
  "`component-s' is a symbol that matches the component's record object
name, i.e SKILL, JOB, EDUCATION, LANGUAGE, CONTACTINFO. This will be
used all over the place. The record name equals the component name.

`rest-args' is the list of REST API arguments for the add and modify
forms. In the form like this example: ((id :parameter-type 'integer))

`link-rest-args' is the the same, but only for the linking form (job
and resume).

`add-fields', `modify-fields' and `link-fields' are lists of generic
form fields that get inserted into the add, modify and link forms. If
`link-fields' is `nil' then the linking form will not be included.

`view-p' is `t' if the component has a view in the database because it
needs to JOIN with other data."
  (let* ((component (symbol-name component-s))
         (component-lcase (string-downcase component))
         (component-s (intern (format nil "~a" component)))
         (table (format nil "resume.~a~a" component-lcase (if view-p "_v" "")))
         (arg-names (loop for arg in rest-args collect (car arg)))
         (link-arg-names (loop for arg in link-rest-args collect (car arg)))
         (func-root-s (intern (format nil "~a-JSON" component)))
         (func-view-s (intern (format nil "~a-VIEW-JSON" component)))
         (func-add-s (intern (format nil "~a-ADD-JSON" component)))
         (func-add-submit-s (intern (format nil "~a-ADD-SUBMIT-JSON" component)))
         (func-modify-s (intern (format nil "~a-MODIFY-JSON" component)))
         (func-modify-submit-s (intern (format nil "~a-MODIFY-SUBMIT-JSON" component)))
         (func-link-s (intern (format nil "~a-LINK-JSON" component)))
         (func-link-submit-s (intern (format nil "~a-LINK-SUBMIT-JSON" component)))
         (func-delete-s (intern (format nil "~a-DELETE-JSON" component)))
         (form-add (format nil "~a-add-form" component-lcase))
         (form-modify (format nil "~a-modify-form" component-lcase))
         (form-link (format nil "~a-link-form" component-lcase)))
    `(progn
       (defun ,func-root-s ()
         (with-auth (instance resume/resume-service "resume-view")
           (setf (title instance) ,component)))

       (defun ,func-view-s ()
         (with-auth (instance resume/resume-component/view-service "resume-view")
           (with-resume-database
             (let ((resume-pkg (make-instance 'resume-pkg))
                   (user (get-user)))
               (setf (components instance) (get-records resume-pkg
                                                        (make-instance ',component-s
                                                                       :user_id (id user)
                                                                       :*table ,table)
                                                        ,order-by))))
           (sanitize-rest-json instance)
           (loop for component in (components instance)
                 do (sanitize-json component))))
       
       (defun ,func-add-s ()
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (with-resume-database
             (let* ((resume-pkg (make-instance 'resume-pkg)))
               (setf (title instance) (format nil "~a - Add" ,component))
               (setf (form instance) (make-form ,form-add
                                                nil
                                                t
                                                ,add-fields))))))

       (defun ,func-add-submit-s (,@arg-names)
         (declare (special ,@arg-names))
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (with-resume-database
             (handler-case
                 (let* ((resume-pkg (make-instance 'resume-pkg))
                        (user (get-user))
                        (,component-s (make-instance ',component-s :user_id (id user))))
                   (loop for param in (remove-if (lambda (x)
                                                   (intersection `(,x) '(id)))
                                                 (sb-introspect:function-lambda-list #',func-add-submit-s))
                         do (setf (slot-value ,component-s param) (symbol-value param)))
                   (insert-record resume-pkg ,component-s)
                   (setf (message instance) (format nil "~a created successfully." ,component)))
               (error (e)
                 (setf (errormsg instance) (format nil "Error creating ~a. ~a" ,component e)))))))

       (defun ,func-modify-s (id)
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (setf (title instance) (format nil "~a - Modify" ,component))
           (with-resume-database
             (let* ((resume-pkg (make-instance 'resume-pkg))
                    (user (get-user))
                    (,component-s (get-record resume-pkg (make-instance ',component-s
                                                                        :id id
                                                                        :user_id (id user)
                                                                        :*table ,table))))
               (if ,component-s
                   (setf (form instance) (make-form ,form-modify
                                                    nil
                                                    t
                                                    ,modify-fields))
                   (setf (errormsg instance) (format nil "Error: could not modify ~a. Not found." ,component)))))))

       (defun ,func-modify-submit-s (,@(append '(id) arg-names))
         (declare (special ,@(append '(id) arg-names)))
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (with-resume-database
             (handler-case
                 (let* ((resume-pkg (make-instance 'resume-pkg))
                        (user (get-user))
                        (,component-s (get-record resume-pkg (make-instance ',component-s
                                                                            :id id
                                                                            :user_id (id user)
                                                                            :*table ,table))))
                   (if ,component-s
                       (let ((new-component (make-instance ',component-s)))
                         (loop for param in (sb-introspect:function-lambda-list #',func-modify-submit-s)
                               do (setf (slot-value new-component param) (symbol-value param)))
                         (setf (user_id new-component) (id user))
                         (update-record-no-nulls resume-pkg new-component)
                         (setf (message instance) (format nil "~a modified successfully." ,component)))
                       (error (format nil "~a error" ,component))))
               (error (e)
                 (setf (errormsg instance) (format nil "Error modifying ~a. ~a" ,component e)))))))

       (defun ,func-link-s (&optional id)
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (setf (title instance) (format nil "~a - Link" ,component))
           (when (> (length ',link-arg-names) 0)
             (with-resume-database
               (let* ((resume-pkg (make-instance 'resume-pkg))
                      (user (get-user))
                      (,component-s (get-record resume-pkg (make-instance ',component-s
                                                                          :id id
                                                                          :user_id (id user)
                                                                          :*table ,table))))
                 (if ,component-s
                     (setf (form instance) (make-form ,form-link
                                                      nil
                                                      t
                                                      ,link-fields))
                     (setf (errormsg instance) (format nil "Error: could not link ~a. Not found." ,component))))))))

       (defun ,func-link-submit-s (&rest args)
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (handler-case
               (progn
                 (apply `,(intern (format nil "LINK-~a" ,component) (intern (package-name #.*package*))) args)
                 (setf (message instance) (format nil "~a linked successfully." ,component)))
             (error (e)
               (setf (errormsg instance) (format nil "Error linking ~a. ~a" ,component e))))))


       (defun ,func-delete-s (id)
         (with-auth (instance resume/resume-component/modify-service "resume-modify")
           (with-resume-database
             (let* ((resume-pkg (make-instance 'resume-pkg))
                    (user (get-user))
                    (,component-s (get-record resume-pkg (make-instance ',component-s
                                                                        :id id
                                                                        :user_id (id user)
                                                                        :*table ,table))))
               (if ,component-s
                   (progn
                     (delete-record resume-pkg (make-instance ',component-s :id id))
                     (setf (message instance) (format nil "~a deleted successfully." ,component)))
                   (progn
                     (setf (errormsg instance) (format nil "Error: could not delete ~a: not found." ,component))))))))

       (define-endpoint (,(format nil "/resume/~a" component-lcase) :method :get) () ,func-root-s)
       (define-endpoint (,(format nil "/resume/~a/view" component-lcase) :method :get) () ,func-view-s)
       (define-endpoint (,(format nil "/resume/~a/add" component-lcase) :method :post) () ,func-add-s)
       (define-endpoint (,(format nil "/resume/~a/add/submit" component-lcase) :method :post) (&post ,@rest-args) ,func-add-submit-s ,@arg-names)
       (define-endpoint (,(format nil "/resume/~a/modify" component-lcase) :method :post) (&post (id :parameter-type 'integer)) ,func-modify-s id)
       (define-endpoint (,(format nil "/resume/~a/modify/submit" component-lcase) :method :post) (&post (id :parameter-type 'integer) ,@rest-args) ,func-modify-submit-s ,@(append '(id) arg-names))
       (define-endpoint (,(format nil "/resume/~a/link" component-lcase) :method :post) (&post (id :parameter-type 'integer)) ,func-link-s id)
       (define-endpoint (,(format nil "/resume/~a/link/submit" component-lcase) :method :post) (&post (id :parameter-type 'integer) ,@link-rest-args) ,func-link-submit-s ,@(append '(id) link-arg-names))
       (define-endpoint (,(format nil "/resume/~a/delete/:id" component-lcase) :method :delete) (&path (id 'integer)) ,func-delete-s id))))

(define-resume-component (:component-s resume
                          :rest-args ((name :parameter-type 'string))
                          :add-fields `((:name "name" :label "Name" :field-type "text" :required "required")
                                        (:label "Add RESUME" :field-type "button" :onclick "on_resume_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id resume) :required "required")
                                           (:name "name" :label "Name" :field-type "text" :value ,(name resume) :required "required")
                                           (:label "Modify RESUME" :field-type "button" :onclick "on_resume_modify_submit_clicked()"))
                          :order-by "name asc"))

(define-resume-component (:component-s job
                          :rest-args ((company :parameter-type 'string) (location :parameter-type 'string) (start_date :parameter-type 'string) (end_date :parameter-type 'string) (overview :parameter-type 'string))
                          :link-rest-args ((skill :parameter-type 'string))
                          :add-fields `((:name "company" :label "Company" :field-type "text" :required "required")
                                        (:name "location" :label "Location" :field-type "text" :required "required")
                                        (:name "start_date" :label "Start Date" :field-type "text" :required "required")
                                        (:name "end_date" :label "End Date" :field-type "text" :required "required")
                                        (:name "overview" :label "Overview" :field-type "textarea" :required "required")
                                        (:label "Add Job" :field-type "button" :onclick "on_job_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id job) :required "required")
                                           (:name "company" :label "Company" :field-type "text" :value ,(company job) :required "required")
                                           (:name "location" :label "Location" :field-type "text" :value ,(location job) :required "required")
                                           (:name "start_date" :label "Start Date" :field-type "text" :value ,(start_date job) :required "required")
                                           (:name "end_date" :label "End Date" :field-type "text" :value ,(end_date job) :required "required")
                                           (:name "overview" :label "Overview" :field-type "textarea" :value ,(overview job) :required "required")
                                           (:label "Modify JOB" :field-type "button" :onclick "on_job_modify_submit_clicked()"))
                          :link-fields `((:name "id" :field-type "hidden" :value ,(id job) :required "required")
                                         ,@(skill-checkboxes resume-pkg (id user) (id job))
                                         (:label "Link Skills to Job" :field-type "button" :onclick "on_job_link_submit_clicked()"))
                          :order-by "end_date desc, start_date desc, company asc"))

(define-resume-component (:component-s contactinfo
                          :rest-args ((address :parameter-type 'string) (city :parameter-type 'string) (state_id :parameter-type  'integer) (phone :parameter-type 'string) (email :parameter-type 'string) (url :parameter-type 'string) (visastatus_id :parameter-type 'integer))
                          :add-fields `((:name "address" :label "Address" :field-type "text" :required "required")
                                        (:name "city" :label "City" :field-type "text" :required "required")
                                        (:name "state_id" :label "State" :field-type "select" :options ,(state-options (get-records resume-pkg (make-instance 'states) "abbr asc")) :required "required")
                                        (:name "phone" :label "Phone" :field-type "text" :required "required")
                                        (:name "email" :label "Email" :field-type "text" :required "required")
                                        (:name "url" :label "URL" :field-type "text")
                                        (:name "visastatus_id" :label "VISA Status" :field-type "select" :options ,(visastatus-options (get-records resume-pkg (make-instance 'visastatus) "visastatus asc")) :required "required")
                                        (:label "Add CONTACTINFO" :field-type "button" :onclick "on_contactinfo_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id contactinfo) :required "required")
                                           (:name "address" :label "Address" :field-type "text" :value ,(address contactinfo) :required "required")
                                           (:name "city" :label "City" :field-type "text" :value ,(city contactinfo) :required "required")
                                           (:name "state_id" :label "State" :field-type "select" :value ,(state_id contactinfo) :options ,(state-options (get-records resume-pkg (make-instance 'states) "abbr asc")) :required "required")
                                           (:name "phone" :label "Phone" :field-type "text" :value ,(phone contactinfo) :required "required")
                                           (:name "email" :label "Email" :field-type "text" :value ,(email contactinfo) :required "required")
                                           (:name "url" :label "URL" :field-type "text" :value ,(url contactinfo))
                                           (:name "visastatus_id" :label "VISA Status" :field-type "select" :value (visastatus_id contactinfo) :options ,(visastatus-options (get-records resume-pkg (make-instance 'visastatus) "visastatus asc")) :required "required")
                                           (:label "Modify CONTACTINFO"  :field-type "button" :onclick "on_contactinfo_modify_submit_clicked()"))
                          :order-by "state_abbr asc, city asc, address asc"
                          :view-p t))

(define-resume-component (:component-s education
                          :rest-args ((school :parameter-type 'string) (major :parameter-type 'string) (minor :parameter-type 'string) (degree :parameter-type 'string) (progress :parameter-type 'string) (start_date :parameter-type 'string) (end_date :parameter-type 'string))
                          :add-fields `((:name "school" :label "School" :field-type "text" :required "required")
                                        (:name "major" :label "Major" :field-type "text" :required "required")
                                        (:name "minor" :label "Minor" :field-type "text")
                                        (:name "degree" :label "Degree" :field-type "text" :required "required")
                                        (:name "progress" :label "Progress" :field-type "text" :required "required")
                                        (:name "start_date" :label "Start Date" :field-type "text" :required "required")
                                        (:name "end_date" :label "End Date" :field-type "text" :required "required")
                                        (:label "Add EDUCATION" :field-type "button" :onclick "on_education_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id education) :required "required")
                                           (:name "school" :label "School" :field-type "text" :value ,(school education) :required "required")
                                           (:name "major" :label "Major" :field-type "text" :value ,(major education) :required "required")
                                           (:name "minor" :label "Minor" :field-type "text" :value ,(minor education))
                                           (:name "degree" :label "Degree" :field-type "text" :value ,(degree education) :required "required")
                                           (:name "progress" :label "Progress" :field-type "text" :value ,(progress education) :required "required")
                                           (:name "start_date" :label "Start Date" :field-type "text" :value ,(start_date education) :required "required")
                                           (:name "end_date" :label "End Date" :field-type "text" :value ,(end_date education) :required "required")
                                           (:label "Modify EDUCATION" :field-type "button" :onclick "on_education_modify_submit_clicked()"))
                          :order-by "end_date desc, start_date desc, school asc, degree asc, major asc, minor asc"))

(define-resume-component (:component-s language
                          :rest-args ((name :parameter-type 'string) (understanding_listening :parameter-type 'string) (understanding_reading :parameter-type 'string) (speaking_interaction :parameter-type 'string) (speaking_production :parameter-type 'string) (writing :parameter-type 'string))
                          :add-fields `((:name "name" :label "Name" :field-type "text" :required "required")
                                        (:name "understanding_listening" :label "Understanding Listening" :field-type "select" :options ,(language-options) :required "required")
                                        (:name "understanding_reading" :label "Understanding Reading" :field-type "select" :options ,(language-options) :required "required")
                                        (:name "speaking_interaction" :label "Speaking Interaction" :field-type "select" :options ,(language-options) :required "required")
                                        (:name "speaking_production" :label "Speaking Production" :field-type "select" :options ,(language-options) :required "required")
                                        (:name "writing" :label "Writing" :field-type "select" :options ,(language-options) :required "required")
                                        (:label "Add LANGUAGE" :field-type "button" :onclick "on_language_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id language) :required "required")
                                           (:name "name" :label "Name" :field-type "text" :value ,(name language) :required "required")
                                           (:name "understanding_listening" :label "Understanding Listening" :field-type "select" :options ,(language-options) :value ,(understanding_listening language) :required "required")
                                           (:name "understanding_reading" :label "Understanding Reading" :field-type "select" :options ,(language-options) :value ,(understanding_reading language) :required "required")
                                           (:name "speaking_interaction" :label "Speaking Interaction" :field-type "select" :options ,(language-options) :value ,(speaking_interaction language) :required "required")
                                           (:name "speaking_production" :label "Speaking Production" :field-type "select" :options ,(language-options) :value ,(speaking_production language) :required "required")
                                           (:name "writing" :label "Writing" :field-type "select" :options ,(language-options) :value ,(writing language) :required "required")
                                           (:label "Modify LANGUAGE" :field-type "button" :onclick "on_language_modify_submit_clicked()"))
                          :order-by "name asc"))

(define-resume-component (:component-s skill
                          :rest-args ((skill :parameter-type 'string))
                          :add-fields `((:name "skill" :label "Skill" :field-type "text" :required "required")
                                        (:label "Add Skill" :field-type "button" :onclick "on_skill_add_submit_clicked()"))
                          :modify-fields `((:name "id" :field-type "hidden" :value ,(id skill) :required "required")
                                           (:name "skill" :label "Skill" :field-type "text" :value ,(skill skill) :required "required")
                                           (:label "Modify SKILL" :field-type "button" :onclick "on_skill_modify_submit_clicked()"))
                          :order-by "skill asc"))