@@ -9,20 +9,34 @@ export const updateService = async (input: FieldResolveInput) =>
9
9
errMiddleware (
10
10
async ( ) => (
11
11
sourceContainUserIdOrThrow ( src ) ,
12
- Promise . all ( args . input . map ( async ( updateSet ) =>
13
- await orm ( ) . then ( ( o ) =>
12
+ Promise . all ( args . input . map ( async ( updateSet ) => {
13
+ const entries = Object . entries ( updateSet || { } ) ;
14
+ const reconstructedObject : Record < string , any > = { } ;
15
+
16
+ const entriesWithOutId = entries . filter ( ( [ key , value ] ) => key !== 'serviceId' && value !== undefined && value !== null ) ;
17
+ if ( ! entriesWithOutId ) {
18
+ throw new Error ( `You need update input argument for this resolver to work` ) ;
19
+ }
20
+
21
+ entriesWithOutId . forEach ( ( entry ) => {
22
+ const [ key , value ] = entry ;
23
+ reconstructedObject [ key ] = value ;
24
+ } ) ;
25
+ return await orm ( ) . then ( ( o ) =>
14
26
o ( 'Services' )
15
27
. collection . updateOne (
16
- { _id : updateSet . serviceId , ownerId : src . userId || src . _id , taken : { $ne : true } , active : { $ne : true } } ,
17
- { $set :
28
+ { _id : updateSet . serviceId , ownerId : src . userId || src . _id , taken : { $ne : true } } ,
29
+ { $set :
18
30
{
19
- ...Object . fromEntries ( Object . entries ( { ...updateSet , startDate : updateSet . startDate ? new Date ( updateSet . startDate as string ) : undefined } ) . filter ( ( e ) => e !== null ) ) ,
20
- updatedAt : new Date ,
21
- } } ,
31
+ ...reconstructedObject ,
32
+ startDate : updateSet . startDate ? new Date ( updateSet . startDate as string ) : undefined ,
33
+ updatedAt : new Date ( ) ,
34
+ } ,
35
+ } ,
22
36
)
23
-
24
- )
25
- ) ) . then ( async ( u ) => u && { service : convertDateObjToStringForArray ( await mustFindAny ( ServicesCollection , { _id : { $in : args . input . map ( ( up ) => up . serviceId ) } } ) ) } )
37
+ )
38
+ }
39
+ ) ) . then ( async ( u ) => u && { service : convertDateObjToStringForArray ( await mustFindAny ( ServicesCollection , { _id : { $in : args . input . map ( ( up ) => up . serviceId ) } } ) ) } )
26
40
) ,
27
41
) ,
28
42
0 commit comments