11const EventsFactory = require ( '../models/eventsFactory' ) ;
2- const { ObjectID } = require ( 'mongodb' ) ;
32const sendPersonalNotification = require ( '../utils/personalNotifications' ) . default ;
43
4+ /**
5+ * Returns a per-request, per-project EventsFactory instance
6+ * Uses context.eventsFactoryCache to memoize by projectId
7+ *
8+ * @param {ResolverContextBase } context - resolver context
9+ * @param {string } projectId - project id to get EventsFactory instance for
10+ * @returns {EventsFactory } - EventsFactory instance bound to a specific project object
11+ */
12+ function getEventsFactoryForProjectId ( context , projectId ) {
13+ const cache = context . eventsFactoryCache || ( context . eventsFactoryCache = new Map ( ) ) ;
14+ const cacheKey = projectId . toString ( ) ;
15+
16+ if ( ! cache . has ( cacheKey ) ) {
17+ cache . set ( cacheKey , new EventsFactory ( projectId ) ) ;
18+ }
19+
20+ return cache . get ( cacheKey ) ;
21+ }
22+
523/**
624 * See all types and fields here {@see ../typeDefs/event.graphql}
725 */
@@ -29,8 +47,8 @@ module.exports = {
2947 *
3048 * @return {RepetitionsPortion }
3149 */
32- async repetitionsPortion ( { projectId, originalEventId } , { limit, cursor } ) {
33- const factory = new EventsFactory ( projectId ) ;
50+ async repetitionsPortion ( { projectId, originalEventId } , { limit, cursor } , context ) {
51+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
3452
3553 return factory . getEventRepetitions ( originalEventId , limit , cursor ) ;
3654 } ,
@@ -84,8 +102,8 @@ module.exports = {
84102 * @param {number } timezoneOffset - user's local timezone offset in minutes
85103 * @returns {Promise<ProjectChartItem[]> }
86104 */
87- async chartData ( { projectId, groupHash } , { days, timezoneOffset } ) {
88- const factory = new EventsFactory ( new ObjectID ( projectId ) ) ;
105+ async chartData ( { projectId, groupHash } , { days, timezoneOffset } , context ) {
106+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
89107
90108 return factory . findChartData ( days , timezoneOffset , groupHash ) ;
91109 } ,
@@ -97,8 +115,8 @@ module.exports = {
97115 * @param {String } eventId - event id
98116 * @returns {Promise<Release> }
99117 */
100- async release ( { projectId, id : eventId } ) {
101- const factory = new EventsFactory ( new ObjectID ( projectId ) ) ;
118+ async release ( { projectId, id : eventId } , _args , context ) {
119+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
102120 const release = await factory . getEventRelease ( eventId ) ;
103121
104122 return release ;
@@ -114,8 +132,8 @@ module.exports = {
114132 * @param {UserInContext } user - user context
115133 * @return {Promise<boolean> }
116134 */
117- async visitEvent ( _obj , { projectId, eventId } , { user } ) {
118- const factory = new EventsFactory ( projectId ) ;
135+ async visitEvent ( _obj , { projectId, eventId } , { user, ... context } ) {
136+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
119137
120138 const { result } = await factory . visitEvent ( eventId , user . id ) ;
121139
@@ -131,8 +149,8 @@ module.exports = {
131149 * @param {string } mark - mark to set
132150 * @return {Promise<boolean> }
133151 */
134- async toggleEventMark ( _obj , { project, eventId, mark } ) {
135- const factory = new EventsFactory ( project ) ;
152+ async toggleEventMark ( _obj , { project, eventId, mark } , context ) {
153+ const factory = getEventsFactoryForProjectId ( context , project ) ;
136154
137155 const { result } = await factory . toggleEventMark ( eventId , mark ) ;
138156
@@ -155,9 +173,9 @@ module.exports = {
155173 * @param factories - factories for working with models
156174 * @return {Promise<boolean> }
157175 */
158- async updateAssignee ( _obj , { input } , { factories, user } ) {
176+ async updateAssignee ( _obj , { input } , { factories, user, ... context } ) {
159177 const { projectId, eventId, assignee } = input ;
160- const factory = new EventsFactory ( projectId ) ;
178+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
161179
162180 const userExists = await factories . usersFactory . findById ( assignee ) ;
163181
@@ -206,9 +224,9 @@ module.exports = {
206224 * @param factories - factories for working with models
207225 * @return {Promise<boolean> }
208226 */
209- async removeAssignee ( _obj , { input } ) {
227+ async removeAssignee ( _obj , { input } , context ) {
210228 const { projectId, eventId } = input ;
211- const factory = new EventsFactory ( projectId ) ;
229+ const factory = getEventsFactoryForProjectId ( context , projectId ) ;
212230
213231 const { result } = await factory . updateAssignee ( eventId , '' ) ;
214232
0 commit comments