1
1
const Medication = require ( "../models/Medication" ) ;
2
2
const User = require ( "../models/UserModel" ) ;
3
+ const DispenseLog = require ( "../models/DispenseLog" ) ;
3
4
const { StatusCodes } = require ( "http-status-codes" ) ;
4
5
const { NotFoundError } = require ( "../errors" ) ;
5
6
6
- // Fetch all medications
7
- const getMedications = async ( req , res ) => {
8
- const medications = await Medication . find ( ) ;
9
- res . status ( StatusCodes . OK ) . json ( { success : true , data : medications } ) ;
10
- console . log ( "Fetch All Medications" ) ;
7
+ const getMedications = async ( req , res , next ) => {
8
+ try {
9
+ const medications = await Medication . find ( ) ;
10
+ res . status ( StatusCodes . OK ) . json ( { success : true , data : medications } ) ;
11
+ } catch ( error ) {
12
+ next ( error ) ;
13
+ }
11
14
} ;
12
15
13
- // Fetch a Single medication
14
- const getMedication = async ( req , res ) => {
15
- const { id } = req . params ;
16
- const medication = await Medication . findById ( id ) ;
17
- if ( ! medication ) {
18
- throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
16
+ const getMedication = async ( req , res , next ) => {
17
+ try {
18
+ const { id } = req . params ;
19
+ const medication = await Medication . findById ( id ) ;
20
+ if ( ! medication ) {
21
+ throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
22
+ }
23
+ res . status ( StatusCodes . OK ) . json ( { success : true , data : medication } ) ;
24
+ } catch ( error ) {
25
+ next ( error ) ;
19
26
}
20
- res . status ( StatusCodes . OK ) . json ( { success : true , data : medication } ) ;
21
- console . log ( "Fetch Single Medication" ) ;
22
27
} ;
23
28
24
- // Create a medication
25
- const createMedication = async ( req , res ) => {
29
+ const createMedication = async ( req , res , next ) => {
26
30
try {
27
- const userId = req . user . id ; // Get the user ID from the request
28
-
29
- // Fetch the user's name using the userId
31
+ const userId = req . user . id ;
30
32
const user = await User . findById ( userId ) ;
31
33
if ( ! user ) {
32
34
return res
33
- . status ( 404 )
35
+ . status ( StatusCodes . NOT_FOUND )
34
36
. json ( { success : false , message : "User not found" } ) ;
35
37
}
36
38
37
- // Assign the user's Name, Id, and Store to the created med
38
39
const medicationData = {
39
40
...req . body ,
40
- loggedBy : user . name , //Assign the user's name
41
- createdBy : user . id , // Assign the user's Id
42
- store : user . store , // Assign the user's store
41
+ createdBy : user . id ,
43
42
} ;
44
-
45
- // Create a new medication record with the user's name
46
43
const medication = await Medication . create ( medicationData ) ;
47
-
48
44
res . status ( StatusCodes . CREATED ) . json ( { success : true , data : medication } ) ;
49
- console . log ( "Create Medication" ) ;
50
45
} catch ( error ) {
51
- res
52
- . status ( StatusCodes . INTERNAL_SERVER_ERROR )
53
- . json ( { success : false , error : error . message } ) ;
54
- console . error ( "Error creating medication:" , error ) ;
46
+ next ( error ) ;
47
+ }
48
+ } ;
49
+
50
+ const dispenseMedication = async ( req , res , next ) => {
51
+ try {
52
+ const { medicationId, quantity } = req . body ;
53
+ const medication = await Medication . findById ( medicationId ) . populate ( {
54
+ path : "createdBy" ,
55
+ select : "store" ,
56
+ } ) ;
57
+
58
+ if ( ! medication ) {
59
+ return res
60
+ . status ( StatusCodes . NOT_FOUND )
61
+ . json ( { success : false , message : "Medication not Found" } ) ;
62
+ }
63
+
64
+ if ( medication . createdBy . store !== req . user . store ) {
65
+ return res . status ( StatusCodes . FORBIDDEN ) . json ( {
66
+ success : false ,
67
+ message :
68
+ "You are not authorized to dispense medication from this store." ,
69
+ } ) ;
70
+ }
71
+
72
+ if ( medication . quantity < quantity ) {
73
+ return res . status ( StatusCodes . BAD_REQUEST ) . json ( {
74
+ success : false ,
75
+ message : "Insufficient stock available" ,
76
+ } ) ;
77
+ }
78
+
79
+ medication . quantity -= quantity ;
80
+ await medication . save ( ) ;
81
+
82
+ const log = await DispenseLog . create ( {
83
+ medicationId,
84
+ userId : req . user . id ,
85
+ quantity,
86
+ dispenseDate : new Date ( ) ,
87
+ } ) ;
88
+
89
+ const populatedLog = await log . populate ( [
90
+ { path : "medicationId" , select : "name ndc lot" } ,
91
+ { path : "userId" , select : "name store" } ,
92
+ ] ) ;
93
+
94
+ res . status ( StatusCodes . OK ) . json ( {
95
+ success : true ,
96
+ message : "Medication dispensed successfully" ,
97
+ log : populatedLog ,
98
+ } ) ;
99
+ } catch ( error ) {
100
+ next ( error ) ;
101
+ }
102
+ } ;
103
+
104
+ const getDispenseLogs = async ( req , res , next ) => {
105
+ try {
106
+ const logs = await DispenseLog . find ( ) ;
107
+ res . status ( StatusCodes . OK ) . json ( { success : true , data : logs } ) ;
108
+ } catch ( error ) {
109
+ next ( error ) ;
55
110
}
56
111
} ;
57
112
58
- // Update an existing medication
59
- const updateMedication = async ( req , res ) => {
60
- const { id } = req . params ;
61
- const medication = await Medication . findByIdAndUpdate ( id , req . body , {
62
- new : true ,
63
- runValidators : true ,
64
- } ) ;
65
- if ( ! medication ) {
66
- throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
113
+ const updateMedication = async ( req , res , next ) => {
114
+ try {
115
+ const { id } = req . params ;
116
+ const medication = await Medication . findByIdAndUpdate ( id , req . body , {
117
+ new : true ,
118
+ runValidators : true ,
119
+ } ) ;
120
+ if ( ! medication ) {
121
+ throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
122
+ }
123
+ res . status ( StatusCodes . OK ) . json ( { success : true , data : medication } ) ;
124
+ } catch ( error ) {
125
+ next ( error ) ;
67
126
}
68
- res . status ( StatusCodes . OK ) . json ( { success : true , data : medication } ) ;
69
- console . log ( "Changes a medication" ) ;
70
127
} ;
71
128
72
- // Delete a medication
73
- const deleteMedication = async ( req , res ) => {
74
- const { id } = req . params ;
75
- const medication = await Medication . findByIdAndDelete ( id ) ;
76
- if ( ! medication ) {
77
- throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
129
+ const deleteMedication = async ( req , res , next ) => {
130
+ try {
131
+ const { id } = req . params ;
132
+ const medication = await Medication . findByIdAndDelete ( id ) ;
133
+ if ( ! medication ) {
134
+ throw new NotFoundError ( `No medication found with id: ${ id } ` ) ;
135
+ }
136
+ res . status ( StatusCodes . OK ) . json ( {
137
+ success : true ,
138
+ message : "Medication deleted successfully" ,
139
+ } ) ;
140
+ } catch ( error ) {
141
+ next ( error ) ;
78
142
}
79
- res
80
- . status ( StatusCodes . OK )
81
- . json ( { success : true , message : "Medication deleted successfully" } ) ;
82
- console . log ( "Delete Medication" ) ;
83
143
} ;
84
144
85
145
module . exports = {
@@ -88,4 +148,6 @@ module.exports = {
88
148
createMedication,
89
149
updateMedication,
90
150
deleteMedication,
151
+ dispenseMedication,
152
+ getDispenseLogs,
91
153
} ;
0 commit comments