@@ -2,27 +2,43 @@ import { FieldResolveInput } from 'stucco-js';
2
2
import { BookStatus , resolverFor } from '../zeus/index.js' ;
3
3
import { orm } from '../utils/db/orm.js' ;
4
4
import { GlobalError , errMiddleware , sourceContainUserIdOrThrow } from '../utils/middleware.js' ;
5
- import { ObjectId } from 'mongodb' ;
5
+ import { ObjectId , WithId } from 'mongodb' ;
6
+ import { ServiceModel } from '../models/ServiceModel.js' ;
6
7
7
8
export const bookService = async ( input : FieldResolveInput ) =>
8
9
resolverFor ( 'UserMutation' , 'bookService' , async ( args , src ) =>
9
10
errMiddleware ( async ( ) => {
10
11
sourceContainUserIdOrThrow ( src ) ;
11
12
const o = await orm ( ) ;
12
13
13
- const services = await Promise . all ( args . input . serviceIds . map ( async ( serviceId ) => {
14
-
15
- const service = await o ( 'Services' ) . collection . findOneAndUpdate (
16
- { _id : serviceId , taken : { $ne : true } } ,
17
- { $set : { taken : true } } ,
18
- ) ;
19
- if ( ! service . value ) {
20
- throw new GlobalError ( `service is already taken: ${ serviceId } ` , import . meta. url ) ;
21
- }
22
-
23
- return service . value
24
- } ) )
25
14
15
+ const bookServices = await Promise . all (
16
+ args . input . serviceIds . map (
17
+ async ( serviceId : string ) =>
18
+ (
19
+ await o ( 'Services' ) . collection . findOneAndUpdate (
20
+ { _id : serviceId , taken : { $ne : true } } ,
21
+ { $set : { taken : true } } ,
22
+ )
23
+ ) . value || serviceId ,
24
+ ) ,
25
+ ) ;
26
+ const [ bookedServices , busy ] = [
27
+ bookServices . filter ( ( s ) : s is WithId < ServiceModel > => typeof s !== 'string' ) ,
28
+ bookServices . filter ( ( s ) : s is string => typeof s === 'string' )
29
+ ] ;
30
+ bookServices . forEach ( async ( s ) => {
31
+ if ( typeof s === 'string' ) {
32
+ await o ( 'Services' ) . collection . updateMany (
33
+ { _id : { $in : bookedServices . map ( ( s : any ) => s . _id ) } } ,
34
+ { $set : { taken : false } } ,
35
+ ) ;
36
+ throw new GlobalError ( `Service is already taken: ${ s } ` , import . meta. url ) ;
37
+ }
38
+ return s . _id ;
39
+ } ) ;
40
+
41
+
26
42
const book = await o ( 'Bookings' )
27
43
. collection . insertOne (
28
44
{
@@ -31,13 +47,13 @@ export const bookService = async (input: FieldResolveInput) =>
31
47
bookerId : src . userId ,
32
48
services : args . input . serviceIds ,
33
49
comments : args . input . comments ? args . input . comments : undefined ,
34
- status : services [ 0 ] . neededAccept ? BookStatus . PENDING : BookStatus . ACCEPTED ,
50
+ status : bookedServices [ 0 ] . neededAccept ? BookStatus . PENDING : BookStatus . ACCEPTED ,
35
51
} )
36
52
. then ( async ( c ) => o ( 'Bookings' ) . collection . findOne ( { _id : c . insertedId } ) ) ;
37
53
if ( ! book ) {
38
54
throw new GlobalError ( 'inserted document is null' , import . meta. url ) ;
39
55
}
40
- return { book : { ...book , services : services } } ;
56
+ return { book : { ...book , services : bookedServices } } ;
41
57
} ) ,
42
58
) ( input . arguments , input . source ) ;
43
59
export default bookService ;
0 commit comments