@@ -890,23 +890,13 @@ class EventsFactory extends Factory {
890890 * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
891891 */
892892 async bulkVisitEvent ( eventIds , userId ) {
893- const unique = [ ...new Set ( ( eventIds || [ ] ) . map ( id => String ( id ) ) ) ] ;
894- const failedEventIds = [ ] ;
895- const validObjectIds = unique . map ( id => new ObjectId ( id ) ) ;
893+ const {
894+ collection,
895+ found,
896+ failedEventIds,
897+ } = await this . _resolveBulkEventsByIds ( eventIds ) ;
896898 const userIdStr = String ( userId ) ;
897899
898- const collection = this . getCollection ( this . TYPES . EVENTS ) ;
899- const found = await collection . find ( { _id : { $in : validObjectIds } } ) . toArray ( ) ;
900- const foundByIdStr = new Map ( found . map ( doc => [ doc . _id . toString ( ) , doc ] ) ) ;
901-
902- for ( const oid of validObjectIds ) {
903- const idStr = oid . toString ( ) ;
904-
905- if ( ! foundByIdStr . has ( idStr ) ) {
906- failedEventIds . push ( idStr ) ;
907- }
908- }
909-
910900 const docsToUpdate = found . filter ( ( doc ) => {
911901 const visitedBy = Array . isArray ( doc . visitedBy ) ? doc . visitedBy : [ ] ;
912902 return ! visitedBy . some ( ( visitedUserId ) => String ( visitedUserId ) === userIdStr ) ;
@@ -977,21 +967,11 @@ class EventsFactory extends Factory {
977967 * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
978968 */
979969 async bulkToggleEventMark ( eventIds , mark ) {
980- const unique = [ ...new Set ( ( eventIds || [ ] ) . map ( id => String ( id ) ) ) ] ;
981- const failedEventIds = [ ] ;
982- const validObjectIds = unique . map ( id => new ObjectId ( id ) ) ;
983-
984- const collection = this . getCollection ( this . TYPES . EVENTS ) ;
985- const found = await collection . find ( { _id : { $in : validObjectIds } } ) . toArray ( ) ;
986- const foundByIdStr = new Map ( found . map ( doc => [ doc . _id . toString ( ) , doc ] ) ) ;
987-
988- for ( const oid of validObjectIds ) {
989- const idStr = oid . toString ( ) ;
990-
991- if ( ! foundByIdStr . has ( idStr ) ) {
992- failedEventIds . push ( idStr ) ;
993- }
994- }
970+ const {
971+ collection,
972+ found,
973+ failedEventIds,
974+ } = await this . _resolveBulkEventsByIds ( eventIds ) ;
995975
996976 const nowSec = Math . floor ( Date . now ( ) / 1000 ) ;
997977 const markKey = `marks.${ mark } ` ;
@@ -1045,21 +1025,11 @@ class EventsFactory extends Factory {
10451025 * @returns {Promise<{ updatedCount: number, updatedEventIds: string[], failedEventIds: string[] }> }
10461026 */
10471027 async bulkUpdateAssignee ( eventIds , assignee ) {
1048- const unique = [ ...new Set ( ( eventIds || [ ] ) . map ( id => String ( id ) ) ) ] ;
1049- const failedEventIds = [ ] ;
1050- const validObjectIds = unique . map ( id => new ObjectId ( id ) ) ;
1051-
1052- const collection = this . getCollection ( this . TYPES . EVENTS ) ;
1053- const found = await collection . find ( { _id : { $in : validObjectIds } } ) . toArray ( ) ;
1054- const foundByIdStr = new Map ( found . map ( doc => [ doc . _id . toString ( ) , doc ] ) ) ;
1055-
1056- for ( const oid of validObjectIds ) {
1057- const idStr = oid . toString ( ) ;
1058-
1059- if ( ! foundByIdStr . has ( idStr ) ) {
1060- failedEventIds . push ( idStr ) ;
1061- }
1062- }
1028+ const {
1029+ collection,
1030+ found,
1031+ failedEventIds,
1032+ } = await this . _resolveBulkEventsByIds ( eventIds ) ;
10631033
10641034 const normalizedAssignee = assignee ? String ( assignee ) : '' ;
10651035 const docsToUpdate = found . filter ( doc => String ( doc . assignee || '' ) !== normalizedAssignee ) ;
@@ -1133,6 +1103,29 @@ class EventsFactory extends Factory {
11331103 return result ;
11341104 }
11351105
1106+ /**
1107+ * Resolve original events for bulk operations and collect not found ids.
1108+ *
1109+ * @param {string[] } eventIds - original event ids
1110+ * @returns {Promise<{ collection: any, found: any[], failedEventIds: string[] }> }
1111+ */
1112+ async _resolveBulkEventsByIds ( eventIds ) {
1113+ const unique = [ ...new Set ( ( eventIds || [ ] ) . map ( id => String ( id ) ) ) ] ;
1114+ const objectIds = unique . map ( id => new ObjectId ( id ) ) ;
1115+ const collection = this . getCollection ( this . TYPES . EVENTS ) ;
1116+ const found = await collection . find ( { _id : { $in : objectIds } } ) . toArray ( ) ;
1117+ const foundByIdStr = new Set ( found . map ( doc => doc . _id . toString ( ) ) ) ;
1118+ const failedEventIds = objectIds
1119+ . map ( id => id . toString ( ) )
1120+ . filter ( id => ! foundByIdStr . has ( id ) ) ;
1121+
1122+ return {
1123+ collection,
1124+ found,
1125+ failedEventIds,
1126+ } ;
1127+ }
1128+
11361129 /**
11371130 * Compose event with repetition
11381131 *
0 commit comments