1
- const mongoose = require ( "mongoose" ) ;
2
- const CooperativeDoctor = require ( '../models/cooperative-doctor.model' ) ;
3
-
1
+ const decode = require ( "jwt-decode" ) ;
2
+ const CooperativeDoctor = require ( "../models/cooperative-doctor.model" ) ;
4
3
5
4
const getCooperativeDoctors = async ( req , res ) => {
6
- let doctors = await CooperativeDoctor . find ( ) . populate ( ' primaryFacility' ) ;
5
+ let doctors = await CooperativeDoctor . find ( ) . populate ( " primaryFacility" ) ;
7
6
8
- if ( doctors ) {
9
- res . status ( 200 ) . json ( doctors ) ;
10
- } else {
11
- res . status ( 400 ) . json ( )
12
- }
13
- }
7
+ if ( doctors ) {
8
+ res . status ( 200 ) . json ( doctors ) ;
9
+ } else {
10
+ res . status ( 400 ) . json ( ) ;
11
+ }
12
+ } ;
14
13
15
14
const getCooperativeDoctor = async ( req , res ) => {
16
- let doctor = await CooperativeDoctor . findById ( req . params . id ) . populate ( 'primaryFacility' ) ;
15
+ let doctor = await CooperativeDoctor . findById ( req . params . id ) . populate (
16
+ "primaryFacility"
17
+ ) ;
17
18
18
- if ( doctor ) {
19
- res . status ( 200 ) . json ( doctor ) ;
20
- } else {
21
- res . status ( 400 ) . json ( )
22
- }
23
- }
19
+ if ( doctor ) {
20
+ res . status ( 200 ) . json ( doctor ) ;
21
+ } else {
22
+ res . status ( 400 ) . json ( ) ;
23
+ }
24
+ } ;
24
25
25
26
const createCooperativeDoctor = async ( req , res ) => {
26
- req . body . _id = new mongoose . Types . ObjectId ( ) ;
27
- req . body . firstname = req . body . fullname . split ( " " ) [ 0 ] ;
28
- req . body . lastname = req . body . fullname . split ( " " ) [ 1 ] ? req . body . fullname . split ( " " ) [ 1 ] : "" ;
29
- req . body . primaryPhone ? req . body . phoneNumbers . push ( req . body . primaryPhone ) : null ;
30
- let doctor = await CooperativeDoctor . create ( req . body ) ;
31
- res . status ( 201 ) . json ( doctor ) ;
32
- }
27
+ var token = decode ( req . headers . authorization ) ;
28
+ req . body . createdBy = token . user . name ;
29
+
30
+ req . body . primaryPhone
31
+ ? req . body . phoneNumbers . push ( req . body . primaryPhone )
32
+ : null ;
33
+ let doctor = await CooperativeDoctor . create ( req . body ) ;
34
+ res . status ( 201 ) . json ( doctor ) ;
35
+ } ;
33
36
34
37
const updateCooperativeDoctor = async ( req , res ) => {
35
- req . body . firstname = req . body . fullname . split ( " " ) [ 0 ] ;
36
- req . body . lastname = req . body . fullname . split ( " " ) [ 1 ] ? req . body . fullname . split ( " " ) [ 1 ] : "" ;
37
- req . body . primaryPhone ? req . body . phoneNumbers = [ req . body . primaryPhone ] : req . body . phoneNumbers = [ ] ;
38
- req . body . secondaryPhones ? req . body . phoneNumbers . push ( req . body . secondaryPhones ) : null ;
39
- let doctor = await CooperativeDoctor . findByIdAndUpdate ( req . params . id , req . body , { new : true } ) ;
40
- res . status ( 200 ) . json ( doctor ) ;
41
- }
38
+ var token = decode ( req . headers . authorization ) ;
39
+ req . body . updatedBy = token . user . name ;
40
+
41
+ req . body . primaryPhone
42
+ ? ( req . body . phoneNumbers = [ req . body . primaryPhone ] )
43
+ : ( req . body . phoneNumbers = [ ] ) ;
44
+ req . body . secondaryPhones
45
+ ? req . body . phoneNumbers . push ( req . body . secondaryPhones )
46
+ : null ;
47
+ let doctor = await CooperativeDoctor . findOneAndUpdate (
48
+ { _id : req . params . id } ,
49
+ req . body ,
50
+ { new : true }
51
+ ) ;
52
+ res . status ( 200 ) . json ( doctor ) ;
53
+ } ;
54
+
55
+ const deleteCooperativeDoctor = async ( req , res ) => {
56
+ await CooperativeDoctor . findByIdAndDelete ( req . params . id ) ;
57
+ res . status ( 200 ) . json ( { message : "Cooperative doctor deleted successfully" } ) ;
58
+ } ;
42
59
43
60
module . exports = {
44
- getCooperativeDoctors, getCooperativeDoctor, createCooperativeDoctor, updateCooperativeDoctor
45
- }
61
+ getCooperativeDoctors,
62
+ getCooperativeDoctor,
63
+ createCooperativeDoctor,
64
+ updateCooperativeDoctor,
65
+ deleteCooperativeDoctor,
66
+ } ;
0 commit comments