@@ -887,7 +887,7 @@ class EventsFactory extends Factory {
887887 *
888888 * @param {string[] } eventIds - original event ids
889889 * @param {string|ObjectId } userId - id of the user who is visiting events
890- * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
890+ * @returns {Promise<{ updatedEventIds: string[], failedEventIds: string[] }> }
891891 */
892892 async bulkVisitEvents ( eventIds , userId ) {
893893 const {
@@ -902,25 +902,53 @@ class EventsFactory extends Factory {
902902
903903 return ! visitedBy . some ( ( visitedUserId ) => String ( visitedUserId ) === userIdStr ) ;
904904 } ) ;
905- const updatedEventIds = docsToUpdate . map ( doc => doc . _id . toString ( ) ) ;
906905
907906 if ( docsToUpdate . length === 0 ) {
908907 return {
909- updatedCount : 0 ,
910908 updatedEventIds : [ ] ,
911909 failedEventIds,
912910 } ;
913911 }
914912
915- const updateManyResult = await collection . updateMany (
916- { _id : { $in : docsToUpdate . map ( doc => doc . _id ) } } ,
917- { $addToSet : { visitedBy : new ObjectId ( userId ) } }
913+ const userObjectId = new ObjectId ( userId ) ;
914+ const settled = await Promise . allSettled (
915+ docsToUpdate . map ( async ( doc ) => {
916+ const eventId = doc . _id . toString ( ) ;
917+ const updateResult = await collection . updateOne (
918+ {
919+ _id : doc . _id ,
920+ visitedBy : { $ne : userObjectId } ,
921+ } ,
922+ { $addToSet : { visitedBy : userObjectId } }
923+ ) ;
924+
925+ return {
926+ eventId,
927+ updated : updateResult . modifiedCount > 0 ,
928+ } ;
929+ } )
918930 ) ;
919931
932+ const updatedEventIds = [ ] ;
933+ const failedByUpdate = [ ] ;
934+
935+ settled . forEach ( ( result , index ) => {
936+ const fallbackEventId = docsToUpdate [ index ] . _id . toString ( ) ;
937+
938+ if ( result . status === 'fulfilled' ) {
939+ if ( result . value . updated ) {
940+ updatedEventIds . push ( result . value . eventId ) ;
941+ } else {
942+ failedByUpdate . push ( result . value . eventId ) ;
943+ }
944+ } else {
945+ failedByUpdate . push ( fallbackEventId ) ;
946+ }
947+ } ) ;
948+
920949 return {
921- updatedCount : updateManyResult . modifiedCount ,
922950 updatedEventIds,
923- failedEventIds,
951+ failedEventIds : [ ... new Set ( [ ... failedEventIds , ... failedByUpdate ] ) ] ,
924952 } ;
925953 }
926954
@@ -965,7 +993,7 @@ class EventsFactory extends Factory {
965993 *
966994 * @param {string[] } eventIds - original event ids
967995 * @param {string } mark - 'resolved' | 'ignored' | 'starred'
968- * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
996+ * @returns {Promise<{ updatedEventIds: string[], failedEventIds: string[] }> }
969997 */
970998 async bulkToggleEventMark ( eventIds , mark ) {
971999 const {
@@ -978,7 +1006,6 @@ class EventsFactory extends Factory {
9781006 const markKey = `marks.${ mark } ` ;
9791007 const allHaveMark = found . length > 0 && found . every ( doc => doc . marks && doc . marks [ mark ] ) ;
9801008 const ops = [ ] ;
981- const updatedEventIds = [ ] ;
9821009
9831010 for ( const doc of found ) {
9841011 const hasMark = doc . marks && doc . marks [ mark ] ;
@@ -998,23 +1025,50 @@ class EventsFactory extends Factory {
9981025 update,
9991026 } ,
10001027 } ) ;
1001- updatedEventIds . push ( doc . _id . toString ( ) ) ;
10021028 }
10031029
10041030 if ( ops . length === 0 ) {
10051031 return {
1006- updatedCount : 0 ,
10071032 updatedEventIds : [ ] ,
10081033 failedEventIds,
10091034 } ;
10101035 }
10111036
1012- const bulkResult = await collection . bulkWrite ( ops , { ordered : false } ) ;
1037+ const settled = await Promise . allSettled (
1038+ ops . map ( async ( { updateOne } ) => {
1039+ const eventId = updateOne . filter . _id . toString ( ) ;
1040+ const updateResult = await collection . updateOne (
1041+ updateOne . filter ,
1042+ updateOne . update
1043+ ) ;
1044+
1045+ return {
1046+ eventId,
1047+ updated : updateResult . modifiedCount > 0 ,
1048+ } ;
1049+ } )
1050+ ) ;
1051+
1052+ const updatedEventIds = [ ] ;
1053+ const failedByUpdate = [ ] ;
1054+
1055+ settled . forEach ( ( result , index ) => {
1056+ const fallbackEventId = ops [ index ] . updateOne . filter . _id . toString ( ) ;
1057+
1058+ if ( result . status === 'fulfilled' ) {
1059+ if ( result . value . updated ) {
1060+ updatedEventIds . push ( result . value . eventId ) ;
1061+ } else {
1062+ failedByUpdate . push ( result . value . eventId ) ;
1063+ }
1064+ } else {
1065+ failedByUpdate . push ( fallbackEventId ) ;
1066+ }
1067+ } ) ;
10131068
10141069 return {
1015- updatedCount : bulkResult . modifiedCount + bulkResult . upsertedCount ,
10161070 updatedEventIds,
1017- failedEventIds,
1071+ failedEventIds : [ ... new Set ( [ ... failedEventIds , ... failedByUpdate ] ) ] ,
10181072 } ;
10191073 }
10201074
@@ -1023,7 +1077,7 @@ class EventsFactory extends Factory {
10231077 *
10241078 * @param {string[] } eventIds - original event ids
10251079 * @param {string|null|undefined } assignee - target assignee id, null/undefined to clear
1026- * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
1080+ * @returns {Promise<{ updatedEventIds: string[], failedEventIds: string[] }> }
10271081 */
10281082 async bulkUpdateAssignee ( eventIds , assignee ) {
10291083 const {
@@ -1034,25 +1088,51 @@ class EventsFactory extends Factory {
10341088
10351089 const normalizedAssignee = assignee ? String ( assignee ) : '' ;
10361090 const docsToUpdate = found . filter ( doc => String ( doc . assignee || '' ) !== normalizedAssignee ) ;
1037- const updatedEventIds = docsToUpdate . map ( doc => doc . _id . toString ( ) ) ;
1038-
10391091 if ( docsToUpdate . length === 0 ) {
10401092 return {
1041- updatedCount : 0 ,
10421093 updatedEventIds : [ ] ,
10431094 failedEventIds,
10441095 } ;
10451096 }
10461097
1047- const updateManyResult = await collection . updateMany (
1048- { _id : { $in : docsToUpdate . map ( doc => doc . _id ) } } ,
1049- { $set : { assignee : normalizedAssignee } }
1098+ const settled = await Promise . allSettled (
1099+ docsToUpdate . map ( async ( doc ) => {
1100+ const eventId = doc . _id . toString ( ) ;
1101+ const updateResult = await collection . updateOne (
1102+ {
1103+ _id : doc . _id ,
1104+ assignee : { $ne : normalizedAssignee } ,
1105+ } ,
1106+ { $set : { assignee : normalizedAssignee } }
1107+ ) ;
1108+
1109+ return {
1110+ eventId,
1111+ updated : updateResult . modifiedCount > 0 ,
1112+ } ;
1113+ } )
10501114 ) ;
10511115
1116+ const updatedEventIds = [ ] ;
1117+ const failedByUpdate = [ ] ;
1118+
1119+ settled . forEach ( ( result , index ) => {
1120+ const fallbackEventId = docsToUpdate [ index ] . _id . toString ( ) ;
1121+
1122+ if ( result . status === 'fulfilled' ) {
1123+ if ( result . value . updated ) {
1124+ updatedEventIds . push ( result . value . eventId ) ;
1125+ } else {
1126+ failedByUpdate . push ( result . value . eventId ) ;
1127+ }
1128+ } else {
1129+ failedByUpdate . push ( fallbackEventId ) ;
1130+ }
1131+ } ) ;
1132+
10521133 return {
1053- updatedCount : updateManyResult . modifiedCount ,
10541134 updatedEventIds,
1055- failedEventIds,
1135+ failedEventIds : [ ... new Set ( [ ... failedEventIds , ... failedByUpdate ] ) ] ,
10561136 } ;
10571137 }
10581138
0 commit comments