11import { ReceiveTypes } from '@hawk.so/types' ;
22import * as telegram from '../utils/telegram' ;
3+ import { DEMO_WORKSPACE_ID } from '../constants/demoWorkspace' ;
34const mongo = require ( '../mongo' ) ;
45const { ObjectId } = require ( 'mongodb' ) ;
56const { ApolloError, UserInputError } = require ( 'apollo-server-express' ) ;
@@ -20,6 +21,54 @@ const GROUPING_TIMESTAMP_INDEX_NAME = 'groupingTimestamp';
2021const GROUPING_TIMESTAMP_AND_LAST_REPETITION_TIME_AND_ID_INDEX_NAME = 'groupingTimestampAndLastRepetitionTimeAndId' ;
2122const GROUPING_TIMESTAMP_AND_GROUP_HASH_INDEX_NAME = 'groupingTimestampAndGroupHash' ;
2223const MAX_SEARCH_QUERY_LENGTH = 50 ;
24+ const FALLBACK_EVENT_TITLE = 'Unknown' ;
25+
26+ /**
27+ * Ensures each daily event has non-empty payload title
28+ * and writes warning log with identifiers when fallback is used.
29+ *
30+ * @param {object } dailyEventsPortion - portion returned by events factory
31+ * @param {string|ObjectId } projectId - project id for logs
32+ * @returns {object }
33+ */
34+ function normalizeDailyEventsPayloadTitle ( dailyEventsPortion , projectId ) {
35+ if ( ! dailyEventsPortion || ! Array . isArray ( dailyEventsPortion . dailyEvents ) ) {
36+ return dailyEventsPortion ;
37+ }
38+
39+ dailyEventsPortion . dailyEvents = dailyEventsPortion . dailyEvents . map ( ( dailyEvent ) => {
40+ const event = dailyEvent && dailyEvent . event ? dailyEvent . event : null ;
41+ const payload = event && event . payload ? event . payload : null ;
42+ const hasValidTitle = payload &&
43+ typeof payload . title === 'string' &&
44+ payload . title . trim ( ) . length > 0 ;
45+
46+ if ( hasValidTitle ) {
47+ return dailyEvent ;
48+ }
49+
50+ console . warn ( '🔴🔴🔴 [ProjectResolver.dailyEventsPortion] Missing event payload title. Fallback title applied.' , {
51+ projectId : projectId ? projectId . toString ( ) : null ,
52+ dailyEventId : dailyEvent && dailyEvent . id ? dailyEvent . id . toString ( ) : null ,
53+ dailyEventGroupHash : dailyEvent && dailyEvent . groupHash ? dailyEvent . groupHash . toString ( ) : null ,
54+ eventOriginalId : event && event . originalEventId ? event . originalEventId . toString ( ) : null ,
55+ eventId : event && event . _id ? event . _id . toString ( ) : null ,
56+ } ) ;
57+
58+ return {
59+ ...dailyEvent ,
60+ event : {
61+ ...( event || { } ) ,
62+ payload : {
63+ ...( payload || { } ) ,
64+ title : FALLBACK_EVENT_TITLE ,
65+ } ,
66+ } ,
67+ } ;
68+ } ) ;
69+
70+ return dailyEventsPortion ;
71+ }
2372
2473/**
2574 * See all types and fields here {@see ../typeDefs/project.graphql}
@@ -205,7 +254,7 @@ module.exports = {
205254 throw new ApolloError ( 'There is no project with that id' ) ;
206255 }
207256
208- if ( project . workspaceId . toString ( ) === '6213b6a01e6281087467cc7a' ) {
257+ if ( project . workspaceId . toString ( ) === DEMO_WORKSPACE_ID ) {
209258 throw new ApolloError ( 'Unable to update demo project' ) ;
210259 }
211260
@@ -243,7 +292,7 @@ module.exports = {
243292 throw new ApolloError ( 'There is no project with that id' ) ;
244293 }
245294
246- if ( project . workspaceId . toString ( ) === '6213b6a01e6281087467cc7a' ) {
295+ if ( project . workspaceId . toString ( ) === DEMO_WORKSPACE_ID ) {
247296 throw new ApolloError ( 'Unable to update demo project' ) ;
248297 }
249298
@@ -351,7 +400,7 @@ module.exports = {
351400 throw new ApolloError ( 'There is no project with that id' ) ;
352401 }
353402
354- if ( project . workspaceId . toString ( ) === '6213b6a01e6281087467cc7a' ) {
403+ if ( project . workspaceId . toString ( ) === DEMO_WORKSPACE_ID ) {
355404 throw new ApolloError ( 'Unable to remove demo project' ) ;
356405 }
357406
@@ -410,7 +459,7 @@ module.exports = {
410459 throw new ApolloError ( 'There is no project with that id' ) ;
411460 }
412461
413- if ( project . workspaceId . toString ( ) === '6213b6a01e6281087467cc7a' ) {
462+ if ( project . workspaceId . toString ( ) === DEMO_WORKSPACE_ID ) {
414463 throw new ApolloError ( 'Unable to update demo project' ) ;
415464 }
416465
@@ -461,7 +510,7 @@ module.exports = {
461510 throw new ApolloError ( 'There is no project with that id' ) ;
462511 }
463512
464- if ( project . workspaceId . toString ( ) === '6213b6a01e6281087467cc7a' ) {
513+ if ( project . workspaceId . toString ( ) === DEMO_WORKSPACE_ID ) {
465514 throw new ApolloError ( 'Unable to update demo project' ) ;
466515 }
467516
@@ -571,17 +620,19 @@ module.exports = {
571620 } ,
572621
573622 /**
574- * Returns recent Events grouped by day
575- *
576- * @param {ProjectDBScheme } project - result of parent resolver
577- * @param {Number } limit - limit for events count
578- * @param {DailyEventsCursor } cursor - object with boundary values of the first event in the next portion
579- * @param {'BY_DATE' | 'BY_COUNT' } sort - events sort order
580- * @param {EventsFilters } filters - marks by which events should be filtered
581- * @param {String } release - release name
582- * @param {String } search - search query
623+ * Returns a paginated portion of daily-grouped events
583624 *
584- * @return {Promise<RecentEventSchema[]> }
625+ * @param {ProjectDBScheme } project - parent resolver result
626+ * @param {object } args - GraphQL arguments
627+ * @param {number } args.limit - max rows in portion
628+ * @param {object|null } args.nextCursor - pagination cursor
629+ * @param {string } args.sort - BY_DATE | BY_COUNT | BY_AFFECTED_USERS (mapped in factory)
630+ * @param {object } args.filters - mark filters only: resolved, starred, ignored (assignee uses args.assignee)
631+ * @param {string } args.search - search query
632+ * @param {string|undefined } args.release - optional release label filter
633+ * @param {string|undefined } args.assignee - user id or __filter_unassigned__ / __filter_any_assignee__
634+ * @param {object } context - GraphQL context
635+ * @returns {Promise<object> } dailyEventsPortion payload from factory
585636 */
586637 async dailyEventsPortion ( project , { limit, nextCursor, sort, filters, search, release, assignee } , context ) {
587638 if ( search ) {
@@ -602,6 +653,8 @@ module.exports = {
602653 assignee
603654 ) ;
604655
656+ normalizeDailyEventsPayloadTitle ( dailyEventsPortion , project . _id ) ;
657+
605658 return dailyEventsPortion ;
606659 } ,
607660
0 commit comments