@messenger-box/platform-server 10.0.3-alpha.7 → 10.0.3-alpha.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/lib/config/env-config.d.ts +7 -0
  2. package/lib/config/env-config.js +20 -0
  3. package/lib/config/env-config.js.map +1 -1
  4. package/lib/containers/containers.js +9 -1
  5. package/lib/containers/containers.js.map +1 -1
  6. package/lib/containers/context-services-from-container.d.ts +1 -1
  7. package/lib/containers/context-services-from-container.js +4 -2
  8. package/lib/containers/context-services-from-container.js.map +1 -1
  9. package/lib/graphql/resolvers/ai-fragment.d.ts +3 -0
  10. package/lib/graphql/resolvers/ai-fragment.js +276 -0
  11. package/lib/graphql/resolvers/ai-fragment.js.map +1 -0
  12. package/lib/graphql/resolvers/channel-member.d.ts +3 -2
  13. package/lib/graphql/resolvers/channel-member.js +30 -5
  14. package/lib/graphql/resolvers/channel-member.js.map +1 -1
  15. package/lib/graphql/resolvers/channel.d.ts +3 -2
  16. package/lib/graphql/resolvers/channel.js +308 -53
  17. package/lib/graphql/resolvers/channel.js.map +1 -1
  18. package/lib/graphql/resolvers/extended-token-account.d.ts +3 -2
  19. package/lib/graphql/resolvers/extended-token-account.js +90 -23
  20. package/lib/graphql/resolvers/extended-token-account.js.map +1 -1
  21. package/lib/graphql/resolvers/index.d.ts +1 -1
  22. package/lib/graphql/resolvers/index.js +1 -1
  23. package/lib/graphql/resolvers/index.js.map +1 -1
  24. package/lib/graphql/resolvers/post-thread.d.ts +1 -1
  25. package/lib/graphql/resolvers/post-thread.js +294 -132
  26. package/lib/graphql/resolvers/post-thread.js.map +1 -1
  27. package/lib/graphql/resolvers/post.d.ts +2 -3
  28. package/lib/graphql/resolvers/post.js +874 -239
  29. package/lib/graphql/resolvers/post.js.map +1 -1
  30. package/lib/graphql/resolvers/reaction.d.ts +3 -2
  31. package/lib/graphql/resolvers/reaction.js +96 -14
  32. package/lib/graphql/resolvers/reaction.js.map +1 -1
  33. package/lib/graphql/schema/ai-fragment.graphql +311 -0
  34. package/lib/graphql/schema/ai-fragment.graphql.js +1 -0
  35. package/lib/graphql/schema/ai-fragment.graphql.js.map +1 -0
  36. package/lib/graphql/schema/channel-member.graphql +110 -21
  37. package/lib/graphql/schema/channel-member.graphql.js +1 -1
  38. package/lib/graphql/schema/channel.graphql +356 -38
  39. package/lib/graphql/schema/channel.graphql.js +1 -1
  40. package/lib/graphql/schema/index.js +2 -2
  41. package/lib/graphql/schema/index.js.map +1 -1
  42. package/lib/graphql/schema/post-thread.graphql +167 -21
  43. package/lib/graphql/schema/post-thread.graphql.js +1 -1
  44. package/lib/graphql/schema/post.graphql +360 -40
  45. package/lib/graphql/schema/post.graphql.js +1 -1
  46. package/lib/graphql/schema/reaction.graphql +71 -13
  47. package/lib/graphql/schema/reaction.graphql.js +1 -1
  48. package/lib/graphql/schema/services.graphql +21 -0
  49. package/lib/graphql/schema/users.graphql +76 -13
  50. package/lib/graphql/schema/users.graphql.js +1 -1
  51. package/lib/index.js +1 -1
  52. package/lib/index.js.map +1 -1
  53. package/lib/inngest/factory.d.ts +20 -0
  54. package/lib/inngest/factory.js +4 -0
  55. package/lib/inngest/factory.js.map +1 -0
  56. package/lib/inngest/functions.d.ts +235 -0
  57. package/lib/inngest/functions.js +1385 -0
  58. package/lib/inngest/functions.js.map +1 -0
  59. package/lib/inngest/index.d.ts +3 -0
  60. package/lib/inngest/prompt.d.ts +6 -0
  61. package/lib/inngest/prompt.js +871 -0
  62. package/lib/inngest/prompt.js.map +1 -0
  63. package/lib/inngest/utils.d.ts +5 -0
  64. package/lib/inngest/utils.js +32 -0
  65. package/lib/inngest/utils.js.map +1 -0
  66. package/lib/interfaces/index.d.ts +0 -1
  67. package/lib/interfaces/services.d.ts +1 -1
  68. package/lib/migrations/dbMigrations/AddPostsConfigurationsMigration.d.ts +17 -0
  69. package/lib/migrations/dbMigrations/AddPostsConfigurationsMigration.js +44 -0
  70. package/lib/migrations/dbMigrations/AddPostsConfigurationsMigration.js.map +1 -0
  71. package/lib/migrations/dbMigrations/index.d.ts +1 -0
  72. package/lib/migrations/index.d.ts +1 -0
  73. package/lib/migrations/mail-template-migration.js +1 -1
  74. package/lib/migrations/message-notification-template-migration.d.ts +1 -1
  75. package/lib/migrations/message-notification-template-migration.js +1 -1
  76. package/lib/module.js +10 -3
  77. package/lib/module.js.map +1 -1
  78. package/lib/plugins/ai-fragment-moleculer-service.d.ts +29 -0
  79. package/lib/plugins/ai-fragment-moleculer-service.js +516 -0
  80. package/lib/plugins/ai-fragment-moleculer-service.js.map +1 -0
  81. package/lib/plugins/channel-moleculer-service.d.ts +21 -1
  82. package/lib/plugins/channel-moleculer-service.js +426 -115
  83. package/lib/plugins/channel-moleculer-service.js.map +1 -1
  84. package/lib/plugins/extended-token-account-moleculer-service.d.ts +25 -1
  85. package/lib/plugins/extended-token-account-moleculer-service.js +348 -22
  86. package/lib/plugins/extended-token-account-moleculer-service.js.map +1 -1
  87. package/lib/plugins/index.d.ts +1 -0
  88. package/lib/plugins/messenger-notification-moleculer-service.d.ts +27 -3
  89. package/lib/plugins/messenger-notification-moleculer-service.js +404 -58
  90. package/lib/plugins/messenger-notification-moleculer-service.js.map +1 -1
  91. package/lib/plugins/post-moleculer-service.d.ts +85 -21
  92. package/lib/plugins/post-moleculer-service.js +1102 -256
  93. package/lib/plugins/post-moleculer-service.js.map +1 -1
  94. package/lib/plugins/post-thread-moleculer-service.d.ts +33 -1
  95. package/lib/plugins/post-thread-moleculer-service.js +326 -8
  96. package/lib/plugins/post-thread-moleculer-service.js.map +1 -1
  97. package/lib/plugins/reaction-moleculer-service.js +1 -1
  98. package/lib/plugins/reaction-moleculer-service.js.map +1 -1
  99. package/lib/preferences/settings/post-settings.d.ts +2 -0
  100. package/lib/preferences/settings/post-settings.js +47 -9
  101. package/lib/preferences/settings/post-settings.js.map +1 -1
  102. package/lib/services/ai-fragment-service.d.ts +195 -0
  103. package/lib/services/ai-fragment-service.js +631 -0
  104. package/lib/services/ai-fragment-service.js.map +1 -0
  105. package/lib/services/channel-service.d.ts +181 -33
  106. package/lib/services/channel-service.js +842 -273
  107. package/lib/services/channel-service.js.map +1 -1
  108. package/lib/services/extended-token-account-service.d.ts +130 -14
  109. package/lib/services/extended-token-account-service.js +462 -52
  110. package/lib/services/extended-token-account-service.js.map +1 -1
  111. package/lib/services/index.d.ts +3 -0
  112. package/lib/services/messenger-notification-service.d.ts +106 -13
  113. package/lib/services/messenger-notification-service.js +824 -442
  114. package/lib/services/messenger-notification-service.js.map +1 -1
  115. package/lib/services/post-service.d.ts +189 -16
  116. package/lib/services/post-service.js +949 -113
  117. package/lib/services/post-service.js.map +1 -1
  118. package/lib/services/post-thread-service.d.ts +114 -5
  119. package/lib/services/post-thread-service.js +400 -13
  120. package/lib/services/post-thread-service.js.map +1 -1
  121. package/lib/services/proxy-services/ai-fragment-microservice.d.ts +23 -0
  122. package/lib/services/proxy-services/ai-fragment-microservice.js +78 -0
  123. package/lib/services/proxy-services/ai-fragment-microservice.js.map +1 -0
  124. package/lib/services/proxy-services/channel-microservice.d.ts +6 -3
  125. package/lib/services/proxy-services/channel-microservice.js +25 -10
  126. package/lib/services/proxy-services/channel-microservice.js.map +1 -1
  127. package/lib/services/proxy-services/index.d.ts +1 -0
  128. package/lib/services/proxy-services/messenger-notification-microservice.d.ts +128 -8
  129. package/lib/services/proxy-services/messenger-notification-microservice.js +324 -29
  130. package/lib/services/proxy-services/messenger-notification-microservice.js.map +1 -1
  131. package/lib/services/proxy-services/post-microservice.d.ts +207 -12
  132. package/lib/services/proxy-services/post-microservice.js +623 -54
  133. package/lib/services/proxy-services/post-microservice.js.map +1 -1
  134. package/lib/services/proxy-services/post-thread-microservice.d.ts +134 -3
  135. package/lib/services/proxy-services/post-thread-microservice.js +388 -6
  136. package/lib/services/proxy-services/post-thread-microservice.js.map +1 -1
  137. package/lib/services/proxy-services/reaction-microservice.d.ts +161 -3
  138. package/lib/services/proxy-services/reaction-microservice.js +474 -2
  139. package/lib/services/proxy-services/reaction-microservice.js.map +1 -1
  140. package/lib/services/reaction-service.d.ts +124 -4
  141. package/lib/services/reaction-service.js +415 -3
  142. package/lib/services/reaction-service.js.map +1 -1
  143. package/lib/services/redis-cache-manager.d.ts +18 -0
  144. package/lib/services/redis-cache-manager.js +83 -0
  145. package/lib/services/redis-cache-manager.js.map +1 -0
  146. package/lib/services/sandbox-error-service.d.ts +23 -0
  147. package/lib/services/sandbox-error-service.js +422 -0
  148. package/lib/services/sandbox-error-service.js.map +1 -0
  149. package/lib/store/models/account-token-store.d.ts +1 -1
  150. package/lib/store/models/account-token-store.js.map +1 -1
  151. package/lib/store/models/ai-fragment.d.ts +4 -0
  152. package/lib/store/models/ai-fragment.js +125 -0
  153. package/lib/store/models/ai-fragment.js.map +1 -0
  154. package/lib/store/models/channel.d.ts +2 -3
  155. package/lib/store/models/channel.js +185 -71
  156. package/lib/store/models/channel.js.map +1 -1
  157. package/lib/store/models/index.d.ts +1 -0
  158. package/lib/store/models/post-thread.d.ts +3 -3
  159. package/lib/store/models/post-thread.js +96 -14
  160. package/lib/store/models/post-thread.js.map +1 -1
  161. package/lib/store/models/post.d.ts +2 -3
  162. package/lib/store/models/post.js +143 -23
  163. package/lib/store/models/post.js.map +1 -1
  164. package/lib/store/models/reaction.d.ts +2 -3
  165. package/lib/store/models/reaction.js +67 -8
  166. package/lib/store/models/reaction.js.map +1 -1
  167. package/lib/store/repositories/__tests__/__fixtures__/team-repository.d.ts +3 -3
  168. package/lib/store/repositories/ai-fragment-repository.d.ts +15 -0
  169. package/lib/store/repositories/ai-fragment-repository.js +69 -0
  170. package/lib/store/repositories/ai-fragment-repository.js.map +1 -0
  171. package/lib/store/repositories/channel-repository.d.ts +6 -6
  172. package/lib/store/repositories/channel-repository.js +5 -2
  173. package/lib/store/repositories/channel-repository.js.map +1 -1
  174. package/lib/store/repositories/index.d.ts +1 -0
  175. package/lib/store/repositories/post-repository.d.ts +6 -6
  176. package/lib/store/repositories/post-repository.js +5 -2
  177. package/lib/store/repositories/post-repository.js.map +1 -1
  178. package/lib/store/repositories/post-thread-repository.d.ts +6 -6
  179. package/lib/store/repositories/post-thread-repository.js +5 -2
  180. package/lib/store/repositories/post-thread-repository.js.map +1 -1
  181. package/lib/store/repositories/reaction-repository.d.ts +6 -6
  182. package/lib/store/repositories/reaction-repository.js +5 -2
  183. package/lib/store/repositories/reaction-repository.js.map +1 -1
  184. package/lib/templates/constants/SERVER_TYPES.ts.template +4 -4
  185. package/lib/templates/repositories/AiFragmentRepository.ts.template +4 -0
  186. package/lib/templates/repositories/ChannelRepository.ts.template +3 -3
  187. package/lib/templates/repositories/PostRepository.ts.template +3 -3
  188. package/lib/templates/repositories/PostThreadRepository.ts.template +3 -3
  189. package/lib/templates/repositories/ReactionRepository.ts.template +3 -4
  190. package/lib/templates/services/AiFragmentService.ts.template +123 -0
  191. package/lib/templates/services/ChannelService.ts.template +290 -39
  192. package/lib/templates/services/ExtendedTokenAccountService.ts.template +104 -9
  193. package/lib/templates/services/MessengerNotificationService.ts.template +94 -19
  194. package/lib/templates/services/PostService.ts.template +265 -20
  195. package/lib/templates/services/PostThreadService.ts.template +151 -6
  196. package/lib/templates/services/ReactionService.ts.template +129 -3
  197. package/lib/templates/services/RedisCacheManager.ts.template +22 -0
  198. package/lib/templates/services/SandboxErrorService.ts.template +125 -0
  199. package/package.json +14 -7
  200. package/lib/interfaces/context.d.ts +0 -14
  201. package/lib/store/models/common-options.js +0 -20
  202. package/lib/store/models/common-options.js.map +0 -1
@@ -1,4 +1,6 @@
1
- import {generateUri}from'@adminide-stack/core';import {RoomType,PostTypeEnum,SortEnum,ClientCacheTypeNames}from'common';import {get}from'lodash-es';import {GraphQLError}from'graphql';// export const postResolvers = ({ pubsub }): IResolvers<IContext> => ({
1
+ import {withFilter}from'graphql-subscriptions';import {RoomType,PostTypeEnum,SortEnum,ConfigCollectionName,Scheme,ClientCacheTypeNames}from'common/server';import {get}from'lodash-es';import {GraphQLError}from'graphql';import {generateResourceUri,DEFAULT_TENANT_ID,buildNodeContext}from'@adminide-stack/core/lib/index.js';// import { IContext } from '../../interfaces';
2
+ // export const postResolvers = ({ pubsub }): IResolvers<IContext> => ({
3
+ // export const postResolvers: (options: IResolverOptions) => IResolvers<IContext> = (options) => ({
2
4
  const postResolvers = options => ({
3
5
  // Temporary fix, until we have extension support in base module
4
6
  FileInfo: {
@@ -7,125 +9,292 @@ const postResolvers = options => ({
7
9
  }, args, {
8
10
  fileInfoService
9
11
  }) => {
10
- const basePath = fileInfoService.awsS3Service.basePath || 'aws';
11
- if (!url.includes(basePath)) {
12
- return url;
13
- }
14
- return fileInfoService.getResizedImage(url, {
15
- width: undefined,
16
- height: undefined
17
- });
12
+ options.logger.trace('(FileInfo.url) url [%s]', url);
13
+ try {
14
+ if (!url) {
15
+ options.logger.warn('Missing URL in FileInfo');
16
+ return null;
17
+ }
18
+ const basePath = fileInfoService.awsS3Service.basePath || 'aws';
19
+ if (!url.includes(basePath)) {
20
+ return url;
21
+ }
22
+ return fileInfoService.getResizedImage(url, {
23
+ width: undefined,
24
+ height: undefined
25
+ });
26
+ } catch (error) {
27
+ options.logger.error('Error getting FileInfo URL: %o', error);
28
+ return url; // Fallback to original URL
29
+ }
18
30
  },
19
31
  extension(src) {
20
- return src.mimeType.split('/')[1];
32
+ options.logger.trace('(FileInfo.extension) mimeType [%s]', src?.mimeType);
33
+ try {
34
+ if (!src?.mimeType || typeof src.mimeType !== 'string') {
35
+ options.logger.warn('Missing or invalid mimeType in FileInfo');
36
+ return null;
37
+ }
38
+ const parts = src.mimeType.split('/');
39
+ return parts.length > 1 ? parts[1] : null;
40
+ } catch (error) {
41
+ options.logger.error('Error getting FileInfo extension: %o', error);
42
+ return null;
43
+ }
21
44
  }
22
45
  },
23
46
  Post: {
24
47
  editedBy(src, args, {
25
- accountService
48
+ accountService,
49
+ accountUserDataLoader
26
50
  }) {
27
- return src?.editedBy ? accountService.findAccountById(src.editedBy.toString()) : null;
51
+ options.logger.trace('(Post.editedBy) editedBy [%s]', src?.editedBy);
52
+ try {
53
+ if (!src?.editedBy) {
54
+ return null;
55
+ }
56
+ return accountUserDataLoader.load(src.editedBy.toString());
57
+ } catch (error) {
58
+ options.logger.error('Error getting Post editedBy: %o', error);
59
+ return null;
60
+ }
28
61
  },
29
62
  deletedBy(src, args, {
30
- accountService
63
+ accountService,
64
+ accountUserDataLoader
31
65
  }) {
32
- return src?.deletedBy ? accountService.findAccountById(src?.deletedBy.toString()) : null;
66
+ options.logger.trace('(Post.deletedBy) deletedBy [%s]', src?.deletedBy);
67
+ try {
68
+ if (!src?.deletedBy) {
69
+ return null;
70
+ }
71
+ return accountUserDataLoader.load(src.deletedBy.toString());
72
+ } catch (error) {
73
+ options.logger.error('Error getting Post deletedBy: %o', error);
74
+ return null;
75
+ }
33
76
  },
34
77
  author(src, args, {
35
78
  accountService
36
79
  }) {
37
- const author = src.author?.toString();
38
- if (author) {
39
- return accountService.findAccountById(author);
80
+ options.logger.trace('(Post.author) author [%s], editedBy [%s]', src?.author, src?.editedBy);
81
+ try {
82
+ const author = src?.author?.toString();
83
+ if (author) {
84
+ return accountService.findAccountById(author);
85
+ }
86
+ // Fallback to editedBy if author is not available
87
+ if (src?.editedBy) {
88
+ return accountService.findAccountById(src.editedBy.toString());
89
+ }
90
+ return null;
91
+ } catch (error) {
92
+ options.logger.error('Error getting Post author: %o', error);
93
+ return null;
40
94
  }
41
- return src?.editedBy ? accountService.findAccountById(src.editedBy.toString()) : null;
42
95
  },
43
96
  channel(src, args, {
44
97
  channelService
45
98
  }) {
46
- return channelService.get(src.channel.toString());
99
+ options.logger.trace('(Post.channel) channel [%s]', src?.channel);
100
+ try {
101
+ if (!src?.channel) {
102
+ options.logger.warn('Missing channel in Post');
103
+ return null;
104
+ }
105
+ return channelService.get(src.channel.toString());
106
+ } catch (error) {
107
+ options.logger.error('Error getting Post channel: %o', error);
108
+ return null;
109
+ }
47
110
  },
48
111
  props(src, _, {
49
112
  userContext
50
113
  }) {
51
- return src?.props?.[`[${userContext.accountId}]`];
114
+ options.logger.trace('(Post.props) accountId [%s]', userContext?.accountId);
115
+ try {
116
+ if (!userContext?.accountId || !src?.props) {
117
+ return null;
118
+ }
119
+ return src.props[`[${userContext.accountId}]`] || null;
120
+ } catch (error) {
121
+ options.logger.error('Error getting Post props: %o', error);
122
+ return null;
123
+ }
52
124
  },
53
125
  isRead(src, _, {
54
126
  userContext
55
127
  }) {
56
- return get(src?.props?.[`[${userContext.accountId}]`], `messenger.posts.isRead`);
128
+ options.logger.trace('(Post.isRead) accountId [%s]', userContext?.accountId);
129
+ try {
130
+ if (!userContext?.accountId || !src?.props) {
131
+ return false;
132
+ }
133
+ return get(src.props[`[${userContext.accountId}]`], 'messenger.posts.isRead', false);
134
+ } catch (error) {
135
+ options.logger.error('Error getting Post isRead: %o', error);
136
+ return false;
137
+ }
57
138
  },
58
139
  isDelivered(src, _, {
59
140
  userContext
60
141
  }) {
61
- return get(src?.props?.[`[${userContext.accountId}]`], `messenger.posts.isDelivered`);
142
+ options.logger.trace('(Post.isDelivered) accountId [%s]', userContext?.accountId);
143
+ try {
144
+ if (!userContext?.accountId || !src?.props) {
145
+ return false;
146
+ }
147
+ return get(src.props[`[${userContext.accountId}]`], 'messenger.posts.isDelivered', false);
148
+ } catch (error) {
149
+ options.logger.error('Error getting Post isDelivered: %o', error);
150
+ return false;
151
+ }
62
152
  },
63
- propsConfiguration(src, args, {
64
- configurationService
153
+ async propsConfiguration(src, args, {
154
+ configurationService,
155
+ userContext
65
156
  }) {
66
- const resource = generateUri('messenger', {
67
- _id: src.id
68
- }, 'props');
69
- let data = '{}';
157
+ options.logger.trace('(Post.propsConfiguration) postId [%s], orgName [%s]', src?.id);
70
158
  try {
71
- data = JSON.stringify(src.props);
72
- } catch (err) {
73
- console.log('---Errr---', err);
74
- }
75
- const machineModel = configurationService.parser(resource.toString(), data);
76
- return {
77
- resource,
78
- contents: machineModel.contents,
79
- overrides: machineModel.overrides,
80
- keys: machineModel.keys,
81
- __typename: ClientCacheTypeNames.MachineConfiguration
82
- };
159
+ // if (!src?.id || !userContext?.orgName) {
160
+ if (!src?.id) {
161
+ options.logger.warn('Missing required data for propsConfiguration');
162
+ throw new Error('Missing post ID or organization name');
163
+ }
164
+ // const { orgName } = userContext;
165
+ // options.logger.debug('Processing propsConfiguration for orgName: %s', orgName);
166
+ const resource = generateResourceUri(ConfigCollectionName.Post, {
167
+ _id: src.id
168
+ }, 'props', DEFAULT_TENANT_ID, {
169
+ scheme: Scheme.CDECODE_RESOURCE
170
+ });
171
+ let data = '{}';
172
+ try {
173
+ data = JSON.stringify(src.props || {});
174
+ } catch (err) {
175
+ options.logger.warn('Error serializing props data: %o', err);
176
+ data = '{}';
177
+ }
178
+ const nodeContext = buildNodeContext(userContext);
179
+ const machineModel = await configurationService.parser(resource.toString(), data, nodeContext, null, false);
180
+ if (machineModel instanceof Error) {
181
+ options.logger.error('Error parsing configuration: %o', machineModel);
182
+ throw machineModel;
183
+ }
184
+ return {
185
+ resource,
186
+ contents: machineModel.contents,
187
+ overrides: machineModel.overrides,
188
+ keys: machineModel.keys,
189
+ __typename: ClientCacheTypeNames.MachineConfiguration
190
+ };
191
+ } catch (error) {
192
+ options.logger.error('Error getting Post propsConfiguration: %o', error);
193
+ throw error;
194
+ }
83
195
  },
84
196
  async files(src, args, {
85
197
  fileInfoService
86
198
  }) {
87
- const {
88
- data,
89
- totalCount
90
- } = await fileInfoService.getAllWithCount({
91
- ...args,
92
- criteria: {
93
- ...args.criteria,
94
- refType: 'Post',
95
- ref: src.id
96
- }
97
- });
98
- return {
99
- data: data.map(({
199
+ options.logger.trace('(Post.files) postId [%s], args [%j]', src?.id, args);
200
+ try {
201
+ if (!src?.id) {
202
+ options.logger.warn('Missing post ID for files');
203
+ return {
204
+ data: [],
205
+ totalCount: 0
206
+ };
207
+ }
208
+ if (!fileInfoService) {
209
+ options.logger.warn('FileInfoService not available');
210
+ return {
211
+ data: [],
212
+ totalCount: 0
213
+ };
214
+ }
215
+ const {
216
+ data,
217
+ totalCount
218
+ } = await fileInfoService.getAllWithCount({
219
+ ...args,
220
+ criteria: {
221
+ ...args.criteria,
222
+ refType: 'Post',
223
+ ref: src.id
224
+ }
225
+ });
226
+ const processedData = data.map(({
100
227
  url,
101
228
  ...rest
102
229
  }) => ({
103
230
  ...rest,
104
231
  url: fileInfoService.getThumbnailImage(url)
105
- })),
106
- totalCount
107
- };
232
+ }));
233
+ options.logger.debug('Retrieved %d files for post %s', processedData.length, src.id);
234
+ return {
235
+ data: processedData,
236
+ totalCount
237
+ };
238
+ } catch (error) {
239
+ options.logger.error('Error getting Post files: %o', error);
240
+ return {
241
+ data: [],
242
+ totalCount: 0
243
+ };
244
+ }
108
245
  },
109
246
  async replies(src, args, {
110
247
  postService
111
248
  }) {
112
- const {
113
- data,
114
- totalCount
115
- } = (await postService?.getAllWithCount({
116
- criteria: {
117
- channel: src.channel.toString(),
118
- parentId: src?.id.toString()
119
- },
120
- sort: {
121
- key: 'createdAt',
122
- value: SortEnum.Desc
249
+ options.logger.trace('(Post.replies) postId [%s], channelId [%s]', src?.id, src?.channel);
250
+ try {
251
+ if (!src?.id || !src?.channel) {
252
+ options.logger.warn('Missing post ID or channel for replies');
253
+ return {
254
+ data: [],
255
+ totalCount: 0
256
+ };
257
+ }
258
+ if (!postService) {
259
+ options.logger.warn('PostService not available');
260
+ return {
261
+ data: [],
262
+ totalCount: 0
263
+ };
123
264
  }
124
- })) ?? [];
125
- return {
126
- totalCount,
127
- data
128
- };
265
+ const {
266
+ data,
267
+ totalCount
268
+ } = (await postService?.getAllWithCount({
269
+ criteria: {
270
+ channel: src.channel.toString(),
271
+ parentId: src.id.toString()
272
+ },
273
+ sort: {
274
+ key: 'createdAt',
275
+ value: SortEnum.Desc
276
+ }
277
+ })) ?? {
278
+ data: [],
279
+ totalCount: 0
280
+ };
281
+ options.logger.debug('Retrieved %d replies for post %s', totalCount, src.id);
282
+ // Set default type to Simple if not specified
283
+ const processedData = data.map(post => ({
284
+ ...post,
285
+ type: (post.type === 'message' ? PostTypeEnum.Message : post.type) || PostTypeEnum.Simple
286
+ }));
287
+ return {
288
+ totalCount,
289
+ data: processedData
290
+ };
291
+ } catch (error) {
292
+ options.logger.error('Error getting Post replies: %o', error);
293
+ return {
294
+ data: [],
295
+ totalCount: 0
296
+ };
297
+ }
129
298
  }
130
299
  },
131
300
  Query: {
@@ -134,31 +303,76 @@ const postResolvers = options => ({
134
303
  parentId,
135
304
  limit,
136
305
  skip,
137
- sort
306
+ sort,
307
+ props
138
308
  }, {
139
309
  postService,
310
+ channelService,
140
311
  userContext
141
312
  }) {
142
- const sortBy = sort || {
143
- key: 'createdAt',
144
- value: SortEnum.Desc
145
- };
146
- const {
147
- data,
148
- totalCount
149
- } = await postService.getAllWithCount({
150
- criteria: {
151
- channel: channelId,
313
+ options.logger.trace('(Query.messages) channelId [%s], parentId [%s], limit [%d], skip [%d]', channelId, parentId, limit, skip);
314
+ try {
315
+ // if (projectId && !channelId) {
316
+ // const { data: channels } = (await channelService.getAllWithCount({
317
+ // criteria: {
318
+ // projectId: projectId?.toString?.(),
319
+ // type: RoomType.Aiassistant,
320
+ // },
321
+ // limit: 1,
322
+ // })) as unknown as { data: IChannel[]; totalCount: number };
323
+ // const channel = channels?.[0];
324
+ // channelId = channel?.id as string;
325
+ // }
326
+ if (!channelId && !props) {
327
+ options.logger.warn('Missing channelId in messages query');
328
+ throw new Error('Channel ID is required');
329
+ }
330
+ if (!userContext?.accountId) {
331
+ options.logger.warn('Missing user context in messages query');
332
+ throw new Error('User authentication required');
333
+ }
334
+ if (!postService) {
335
+ options.logger.warn('PostService not available');
336
+ throw new Error('Post service unavailable');
337
+ }
338
+ const sortBy = sort || {
339
+ key: 'createdAt',
340
+ value: SortEnum.Desc
341
+ };
342
+ const channel = channelId || undefined;
343
+ // const projectId = props?.projectId ? `"props.projectId":"${props?.projectId}"` : undefined;
344
+ // console.log("server projectId",projectId)
345
+ const criteria = props ? {
346
+ channel,
347
+ parentId,
348
+ 'props.projectId': props?.projectId || undefined
349
+ } : {
350
+ channel,
152
351
  parentId
153
- },
154
- limit,
155
- skip,
156
- sort: sortBy
157
- });
158
- return {
159
- totalCount,
160
- data
161
- };
352
+ };
353
+ const {
354
+ data,
355
+ totalCount
356
+ } = await postService.getAllWithCount({
357
+ criteria,
358
+ limit,
359
+ skip,
360
+ sort: sortBy
361
+ });
362
+ options.logger.debug('Retrieved %d messages for channel %s', totalCount, channelId);
363
+ // Set default type to Simple if not specified
364
+ const processedData = data.map(post => ({
365
+ ...post,
366
+ type: (post.type === 'message' ? PostTypeEnum.Message : post.type) || PostTypeEnum.Simple
367
+ }));
368
+ return {
369
+ totalCount,
370
+ data: processedData
371
+ };
372
+ } catch (error) {
373
+ options.logger.error('Error in messages query: %o', error);
374
+ throw error;
375
+ }
162
376
  },
163
377
  async publicMessages(src, {
164
378
  channelId
@@ -166,23 +380,43 @@ const postResolvers = options => ({
166
380
  postService,
167
381
  userContext
168
382
  }) {
169
- const {
170
- data,
171
- totalCount
172
- } = await postService.getAllWithCount({
173
- criteria: {
174
- channel: channelId,
175
- type: PostTypeEnum.Public
176
- },
177
- sort: {
178
- key: 'createdAt',
179
- value: SortEnum.Desc
383
+ options.logger.trace('(Query.publicMessages) channelId [%s]', channelId);
384
+ try {
385
+ if (!channelId) {
386
+ options.logger.warn('Missing channelId in publicMessages query');
387
+ throw new Error('Channel ID is required');
388
+ }
389
+ if (!postService) {
390
+ options.logger.warn('PostService not available');
391
+ throw new Error('Post service unavailable');
180
392
  }
181
- });
182
- return {
183
- totalCount,
184
- data
185
- };
393
+ const {
394
+ data,
395
+ totalCount
396
+ } = await postService.getAllWithCount({
397
+ criteria: {
398
+ channel: channelId,
399
+ type: PostTypeEnum.Public
400
+ },
401
+ sort: {
402
+ key: 'createdAt',
403
+ value: SortEnum.Desc
404
+ }
405
+ });
406
+ options.logger.debug('Retrieved %d public messages for channel %s', totalCount, channelId);
407
+ // Set default type to Simple if not specified
408
+ const processedData = data.map(post => ({
409
+ ...post,
410
+ type: post.type || PostTypeEnum.Simple
411
+ }));
412
+ return {
413
+ totalCount,
414
+ data: processedData
415
+ };
416
+ } catch (error) {
417
+ options.logger.error('Error in publicMessages query: %o', error);
418
+ throw error;
419
+ }
186
420
  }
187
421
  },
188
422
  Mutation: {
@@ -191,14 +425,29 @@ const postResolvers = options => ({
191
425
  }, {
192
426
  postService
193
427
  }) {
194
- const {
195
- channelId,
196
- messageId: id
197
- } = messageId;
198
- return postService.delete({
199
- channel: channelId,
200
- id
201
- });
428
+ options.logger.trace('(Mutation.deleteMessage) messageId [%j]', messageId);
429
+ try {
430
+ if (!messageId?.channelId || !messageId?.messageId) {
431
+ options.logger.warn('Missing required messageId parameters');
432
+ throw new Error('Channel ID and Message ID are required');
433
+ }
434
+ if (!postService) {
435
+ options.logger.warn('PostService not available');
436
+ throw new Error('Post service unavailable');
437
+ }
438
+ const {
439
+ channelId,
440
+ messageId: id
441
+ } = messageId;
442
+ options.logger.debug('Deleting message %s from channel %s', id, channelId);
443
+ return postService.delete({
444
+ channel: channelId,
445
+ id
446
+ });
447
+ } catch (error) {
448
+ options.logger.error('Error deleting message: %o', error);
449
+ throw error;
450
+ }
202
451
  },
203
452
  editMessage(src, {
204
453
  messageId,
@@ -207,12 +456,45 @@ const postResolvers = options => ({
207
456
  postService,
208
457
  userContext
209
458
  }) {
210
- return postService.update(messageId.messageId, {
211
- message: messageInput.content,
212
- editedBy: userContext.accountId,
213
- channel: messageId.channelId,
214
- files: messageInput.files
215
- });
459
+ options.logger.trace('(Mutation.editMessage) messageId [%j], content length [%d]', messageId, messageInput?.content?.length || 0);
460
+ try {
461
+ if (!messageId?.messageId || !messageId?.channelId) {
462
+ options.logger.warn('Missing required messageId parameters');
463
+ throw new Error('Message ID and Channel ID are required');
464
+ }
465
+ if (!messageInput?.content) {
466
+ options.logger.warn('Missing message content');
467
+ throw new Error('Message content is required');
468
+ }
469
+ if (!userContext?.accountId) {
470
+ options.logger.warn('Missing user context');
471
+ throw new Error('User authentication required');
472
+ }
473
+ if (!postService) {
474
+ options.logger.warn('PostService not available');
475
+ throw new Error('Post service unavailable');
476
+ }
477
+ options.logger.debug('Editing message %s in channel %s', messageId.messageId, messageId.channelId);
478
+ const {
479
+ content,
480
+ files,
481
+ extraProps
482
+ } = messageInput;
483
+ return postService.update(messageId.messageId, {
484
+ message: content,
485
+ editedBy: userContext.accountId,
486
+ channel: messageId.channelId,
487
+ files,
488
+ ...(extraProps && {
489
+ props: {
490
+ extraProps
491
+ }
492
+ })
493
+ });
494
+ } catch (error) {
495
+ options.logger.error('Error editing message: %o', error);
496
+ throw error;
497
+ }
216
498
  },
217
499
  async sendMessage(src, {
218
500
  channelId,
@@ -222,49 +504,69 @@ const postResolvers = options => ({
222
504
  postService,
223
505
  userContext
224
506
  }) {
225
- const {
226
- content,
227
- files,
228
- createdBy,
229
- notificationParams
230
- } = messageInput;
231
- const {
232
- accountId
233
- } = userContext;
234
- let post;
235
- if (postId) {
236
- post = await postService.create({
237
- _id: postId,
238
- channel: channelId,
239
- message: content,
240
- editedBy: createdBy || accountId,
241
- author: createdBy || accountId,
507
+ options.logger.trace('(Mutation.sendMessage) channelId [%s], postId [%s], content length [%d]', channelId, postId, messageInput?.content?.length || 0);
508
+ try {
509
+ if (!channelId) {
510
+ options.logger.warn('Missing channelId');
511
+ throw new Error('Channel ID is required');
512
+ }
513
+ if (!messageInput?.content) {
514
+ options.logger.warn('Missing message content');
515
+ throw new Error('Message content is required');
516
+ }
517
+ if (!userContext?.accountId) {
518
+ options.logger.warn('Missing user context');
519
+ throw new Error('User authentication required');
520
+ }
521
+ if (!postService) {
522
+ options.logger.warn('PostService not available');
523
+ throw new Error('Post service unavailable');
524
+ }
525
+ const {
526
+ content,
242
527
  files,
243
- props: notificationParams ? {
244
- notificationParams
245
- } : undefined
246
- });
247
- } else {
248
- post = await postService.create({
528
+ createdBy,
529
+ notificationParams,
530
+ type,
531
+ extraProps
532
+ } = messageInput;
533
+ const {
534
+ accountId
535
+ } = userContext;
536
+ const postData = {
249
537
  channel: channelId,
250
538
  message: content,
251
539
  editedBy: createdBy || accountId,
252
540
  author: createdBy || accountId,
253
541
  files,
254
- props: notificationParams ? {
255
- notificationParams
256
- } : undefined
257
- });
542
+ props: notificationParams || extraProps ? {
543
+ ...(notificationParams && {
544
+ notificationParams
545
+ }),
546
+ ...(extraProps && {
547
+ ...extraProps
548
+ })
549
+ } : undefined,
550
+ type: type || PostTypeEnum.Simple
551
+ };
552
+ let post;
553
+ if (postId) {
554
+ options.logger.debug('Creating message with specific ID %s', postId);
555
+ post = await postService.create({
556
+ _id: postId,
557
+ ...postData
558
+ });
559
+ } else {
560
+ options.logger.debug('Creating new message in channel %s', channelId);
561
+ post = await postService.create(postData);
562
+ }
563
+ options.logger.debug('Message created successfully with ID %s', post?.id || post?._id);
564
+ //options.pubsub.publish(`POST_CREATED.${channelId}`, post);
565
+ return post;
566
+ } catch (error) {
567
+ options.logger.error('Error sending message: %o', error);
568
+ throw error;
258
569
  }
259
- // const post = await postService.create({
260
- // channel: channelId as any,
261
- // message: content,
262
- // editedBy: accountId as any,
263
- // author: accountId as any,
264
- // files,
265
- // });
266
- // options.pubsub.publish(`POST_CREATED.${channelId}`, post);
267
- return post;
268
570
  },
269
571
  async sendPublicMessage(src, {
270
572
  channelId,
@@ -275,46 +577,77 @@ const postResolvers = options => ({
275
577
  channelService,
276
578
  userContext
277
579
  }) {
278
- const {
279
- content,
280
- files,
281
- createdBy
282
- } = messageInput;
283
- const {
284
- accountId
285
- } = userContext;
286
- let post = null;
287
- const channelExits = await channelService.get(channelId);
288
- if (!channelExits) {
289
- const channel = await channelService.savePublicChannel({
290
- _id: channelId,
291
- type: RoomType.Public,
292
- title: `public-channel-${channelId}`
293
- });
294
- if (channel) {
295
- post = await postService.createWithoutSubscription({
296
- _id: postId || undefined,
297
- channel: channelId,
298
- message: content,
299
- editedBy: createdBy || accountId,
300
- author: createdBy || accountId,
301
- files,
302
- type: PostTypeEnum.Public
580
+ options.logger.trace('(Mutation.sendPublicMessage) channelId [%s], postId [%s]', channelId, postId);
581
+ try {
582
+ if (!channelId) {
583
+ options.logger.warn('Missing channelId');
584
+ throw new Error('Channel ID is required');
585
+ }
586
+ if (!messageInput?.content) {
587
+ options.logger.warn('Missing message content');
588
+ throw new Error('Message content is required');
589
+ }
590
+ if (!userContext?.accountId) {
591
+ options.logger.warn('Missing user context');
592
+ throw new Error('User authentication required');
593
+ }
594
+ if (!postService || !channelService) {
595
+ options.logger.warn('Required services not available');
596
+ throw new Error('Required services unavailable');
597
+ }
598
+ const {
599
+ content,
600
+ files,
601
+ createdBy,
602
+ extraProps
603
+ } = messageInput;
604
+ const {
605
+ accountId
606
+ } = userContext;
607
+ let post = null;
608
+ const channelExists = await channelService.get(channelId);
609
+ if (!channelExists) {
610
+ options.logger.debug('Creating public channel %s', channelId);
611
+ const channel = await channelService.savePublicChannel({
612
+ _id: channelId,
613
+ type: RoomType.Public,
614
+ title: `public-channel-${channelId}`
303
615
  });
616
+ if (!channel) {
617
+ options.logger.error('Failed to create public channel %s', channelId);
618
+ throw new Error('Failed to create public channel');
619
+ }
304
620
  }
305
- } else {
306
- post = await postService.createWithoutSubscription({
307
- _id: postId || undefined,
621
+ const postData = {
308
622
  channel: channelId,
309
623
  message: content,
310
624
  editedBy: createdBy || accountId,
311
625
  author: createdBy || accountId,
312
626
  files,
313
- type: PostTypeEnum.Public
314
- });
627
+ type: PostTypeEnum.Public,
628
+ ...(extraProps && {
629
+ props: {
630
+ extraProps
631
+ }
632
+ })
633
+ };
634
+ if (postId) {
635
+ post = await postService.createWithoutSubscription({
636
+ _id: postId,
637
+ ...postData
638
+ });
639
+ } else {
640
+ post = await postService.createWithoutSubscription(postData);
641
+ }
642
+ if (post) {
643
+ options.logger.debug('Publishing public post created event for channel %s', channelId);
644
+ options.pubsub.publish(`PUBLIC_POST_CREATED.${channelId}`, post);
645
+ }
646
+ return post;
647
+ } catch (error) {
648
+ options.logger.error('Error sending public message: %o', error);
649
+ throw error;
315
650
  }
316
- if (post) options.pubsub.publish(`PUBLIC_POST_CREATED.${channelId}`, post);
317
- return post;
318
651
  },
319
652
  async sendExpoNotificationOnPost(_, {
320
653
  postId,
@@ -324,9 +657,29 @@ const postResolvers = options => ({
324
657
  postService,
325
658
  userContext
326
659
  }) {
327
- const post = await postService.get(postId.toString());
328
- const res = await messengerNotificationService.sendExpoNotificationOnPost(post, notificationData);
329
- return res;
660
+ options.logger.trace('(Mutation.sendExpoNotificationOnPost) postId [%s]', postId);
661
+ try {
662
+ if (!postId) {
663
+ options.logger.warn('Missing postId');
664
+ throw new Error('Post ID is required');
665
+ }
666
+ if (!messengerNotificationService || !postService) {
667
+ options.logger.warn('Required services not available');
668
+ throw new Error('Required services unavailable');
669
+ }
670
+ const post = await postService.get(postId.toString());
671
+ if (!post) {
672
+ options.logger.warn('Post not found: %s', postId);
673
+ throw new Error('Post not found');
674
+ }
675
+ options.logger.debug('Sending expo notification for post %s', postId);
676
+ const result = await messengerNotificationService.sendExpoNotificationOnPost(post, notificationData);
677
+ options.logger.debug('Expo notification sent successfully for post %s', postId);
678
+ return result;
679
+ } catch (error) {
680
+ options.logger.error('Error sending expo notification: %o', error);
681
+ throw error;
682
+ }
330
683
  },
331
684
  createMessageFileUploadLink(_, {
332
685
  postId,
@@ -335,7 +688,25 @@ const postResolvers = options => ({
335
688
  postService,
336
689
  userContext
337
690
  }) {
338
- return postService.createFileUploadLink(postId, filename, userContext.accountId);
691
+ options.logger.trace('(Mutation.createMessageFileUploadLink) postId [%s], filename [%s]', postId, filename);
692
+ try {
693
+ if (!postId || !filename) {
694
+ options.logger.warn('Missing postId or filename');
695
+ throw new Error('Post ID and filename are required');
696
+ }
697
+ if (!userContext?.accountId) {
698
+ options.logger.warn('Missing user context');
699
+ throw new Error('User authentication required');
700
+ }
701
+ if (!postService) {
702
+ options.logger.warn('PostService not available');
703
+ throw new Error('Post service unavailable');
704
+ }
705
+ return postService.createFileUploadLink(postId, filename, userContext.accountId);
706
+ } catch (error) {
707
+ options.logger.error('Error creating file upload link: %o', error);
708
+ throw error;
709
+ }
339
710
  },
340
711
  attachUploadedFileToMessage(_, {
341
712
  postId,
@@ -344,7 +715,25 @@ const postResolvers = options => ({
344
715
  postService,
345
716
  userContext
346
717
  }) {
347
- return postService.attachUploadedFile(postId, file, userContext.accountId);
718
+ options.logger.trace('(Mutation.attachUploadedFileToMessage) postId [%s], filename [%s]', postId, file?.name);
719
+ try {
720
+ if (!postId || !file) {
721
+ options.logger.warn('Missing postId or file');
722
+ throw new Error('Post ID and file are required');
723
+ }
724
+ if (!userContext?.accountId) {
725
+ options.logger.warn('Missing user context');
726
+ throw new Error('User authentication required');
727
+ }
728
+ if (!postService) {
729
+ options.logger.warn('PostService not available');
730
+ throw new Error('Post service unavailable');
731
+ }
732
+ return postService.attachUploadedFile(postId, file, userContext.accountId);
733
+ } catch (error) {
734
+ options.logger.error('Error attaching uploaded file: %o', error);
735
+ throw error;
736
+ }
348
737
  },
349
738
  async createMessageFilesUploadLink(_, {
350
739
  postId,
@@ -353,7 +742,25 @@ const postResolvers = options => ({
353
742
  postService,
354
743
  userContext
355
744
  }) {
356
- return postService.createFilesUploadLink(postId, filenames, userContext.accountId);
745
+ options.logger.trace('(Mutation.createMessageFilesUploadLink) postId [%s], fileCount [%d]', postId, filenames?.length || 0);
746
+ try {
747
+ if (!postId || !filenames || !Array.isArray(filenames) || filenames.length === 0) {
748
+ options.logger.warn('Missing postId or filenames');
749
+ throw new Error('Post ID and filenames array are required');
750
+ }
751
+ if (!userContext?.accountId) {
752
+ options.logger.warn('Missing user context');
753
+ throw new Error('User authentication required');
754
+ }
755
+ if (!postService) {
756
+ options.logger.warn('PostService not available');
757
+ throw new Error('Post service unavailable');
758
+ }
759
+ return postService.createFilesUploadLink(postId, filenames, userContext.accountId);
760
+ } catch (error) {
761
+ options.logger.error('Error creating files upload links: %o', error);
762
+ throw error;
763
+ }
357
764
  },
358
765
  async attachUploadedFilesToMessage(_, {
359
766
  postId,
@@ -362,14 +769,46 @@ const postResolvers = options => ({
362
769
  postService,
363
770
  userContext
364
771
  }) {
365
- return postService.attachUploadedFiles(postId, files, userContext.accountId);
772
+ options.logger.trace('(Mutation.attachUploadedFilesToMessage) postId [%s], fileCount [%d]', postId, files?.length || 0);
773
+ try {
774
+ if (!postId || !files || !Array.isArray(files) || files.length === 0) {
775
+ options.logger.warn('Missing postId or files');
776
+ throw new Error('Post ID and files array are required');
777
+ }
778
+ if (!userContext?.accountId) {
779
+ options.logger.warn('Missing user context');
780
+ throw new Error('User authentication required');
781
+ }
782
+ if (!postService) {
783
+ options.logger.warn('PostService not available');
784
+ throw new Error('Post service unavailable');
785
+ }
786
+ return postService.attachUploadedFiles(postId, files, userContext.accountId);
787
+ } catch (error) {
788
+ options.logger.error('Error attaching uploaded files: %o', error);
789
+ throw error;
790
+ }
366
791
  },
367
792
  deleteMessageFile(_, {
368
793
  url
369
794
  }, {
370
795
  postService
371
796
  }) {
372
- return postService.deleteFile(url);
797
+ options.logger.trace('(Mutation.deleteMessageFile) url [%s]', url);
798
+ try {
799
+ if (!url) {
800
+ options.logger.warn('Missing URL');
801
+ throw new Error('File URL is required');
802
+ }
803
+ if (!postService) {
804
+ options.logger.warn('PostService not available');
805
+ throw new Error('Post service unavailable');
806
+ }
807
+ return postService.deleteFile(url);
808
+ } catch (error) {
809
+ options.logger.error('Error deleting message file: %o', error);
810
+ throw error;
811
+ }
373
812
  },
374
813
  async readMessage(src, {
375
814
  messageId
@@ -378,11 +817,30 @@ const postResolvers = options => ({
378
817
  userContext,
379
818
  channelService
380
819
  }) {
381
- const member = await channelService.isMember(messageId.channelId, userContext.accountId);
382
- if (!member) {
383
- throw new GraphQLError('Message does not belong to user');
820
+ options.logger.trace('(Mutation.readMessage) messageId [%j], accountId [%s]', messageId, userContext?.accountId);
821
+ try {
822
+ if (!messageId?.channelId || !messageId?.messageId) {
823
+ options.logger.warn('Missing messageId parameters');
824
+ throw new GraphQLError('Message ID and Channel ID are required');
825
+ }
826
+ if (!userContext?.accountId) {
827
+ options.logger.warn('Missing user context');
828
+ throw new GraphQLError('User authentication required');
829
+ }
830
+ if (!postService || !channelService) {
831
+ options.logger.warn('Required services not available');
832
+ throw new GraphQLError('Required services unavailable');
833
+ }
834
+ const member = await channelService.isMember(messageId.channelId, userContext.accountId);
835
+ if (!member) {
836
+ options.logger.warn('User %s is not a member of channel %s', userContext.accountId, messageId.channelId);
837
+ throw new GraphQLError('Message does not belong to user');
838
+ }
839
+ return postService.readMessage(messageId, userContext.accountId);
840
+ } catch (error) {
841
+ options.logger.error('Error reading message: %o', error);
842
+ throw error;
384
843
  }
385
- return postService.readMessage(messageId, userContext.accountId);
386
844
  },
387
845
  async deliverMessage(src, {
388
846
  messageId
@@ -391,70 +849,247 @@ const postResolvers = options => ({
391
849
  userContext,
392
850
  channelService
393
851
  }) {
394
- const member = await channelService.isMember(messageId.channelId, userContext.accountId);
395
- if (!member) {
396
- throw new GraphQLError('Message does not belong to user');
852
+ options.logger.trace('(Mutation.deliverMessage) messageId [%j], accountId [%s]', messageId, userContext?.accountId);
853
+ try {
854
+ if (!messageId?.channelId || !messageId?.messageId) {
855
+ options.logger.warn('Missing messageId parameters');
856
+ throw new GraphQLError('Message ID and Channel ID are required');
857
+ }
858
+ if (!userContext?.accountId) {
859
+ options.logger.warn('Missing user context');
860
+ throw new GraphQLError('User authentication required');
861
+ }
862
+ if (!postService || !channelService) {
863
+ options.logger.warn('Required services not available');
864
+ throw new GraphQLError('Required services unavailable');
865
+ }
866
+ const member = await channelService.isMember(messageId.channelId, userContext.accountId);
867
+ if (!member) {
868
+ options.logger.warn('User %s is not a member of channel %s', userContext.accountId, messageId.channelId);
869
+ throw new GraphQLError('Message does not belong to user');
870
+ }
871
+ return postService.deliverMessage(messageId, userContext.accountId);
872
+ } catch (error) {
873
+ options.logger.error('Error delivering message: %o', error);
874
+ throw error;
875
+ }
876
+ },
877
+ async generateAiCode(src, {
878
+ messageId,
879
+ modelConfig
880
+ }, {
881
+ postService,
882
+ userContext,
883
+ inngestClient
884
+ }) {
885
+ options.logger.trace('(Mutation.generateAiCode) messageId [%s], modelConfig [%j]', messageId, modelConfig);
886
+ try {
887
+ if (!messageId || !modelConfig) {
888
+ options.logger.warn('Missing messageId or modelConfig');
889
+ throw new Error('Message ID and model configuration are required');
890
+ }
891
+ const post = await postService.get(messageId);
892
+ if (!post) {
893
+ options.logger.warn('Post not found: %s', messageId);
894
+ throw new Error('Post not found');
895
+ }
896
+ const result = await inngestClient.send({
897
+ name: 'ai-code-agent/run-code-generation',
898
+ data: {
899
+ messageId: post.id,
900
+ modelConfig: modelConfig
901
+ }
902
+ });
903
+ return {
904
+ success: true,
905
+ message: 'Code generation initiated successfully',
906
+ eventId: result.ids[0]
907
+ };
908
+ } catch (error) {
909
+ options.logger.error('Error generating AI code: %o', error);
910
+ throw error;
911
+ }
912
+ },
913
+ async recreateSandbox(src, {
914
+ messageId,
915
+ projectId
916
+ }, {
917
+ postService,
918
+ userContext,
919
+ inngestClient
920
+ }) {
921
+ options.logger.trace('(Mutation.recreateSandbox) messageId [%s], projectId [%s]', messageId, projectId);
922
+ try {
923
+ const result = await inngestClient.send({
924
+ name: 'recreate-sandbox-url',
925
+ data: {
926
+ messageId,
927
+ projectId
928
+ }
929
+ });
930
+ return {
931
+ success: true,
932
+ message: 'Sandbox recreation initiated successfully',
933
+ eventId: result.ids[0]
934
+ };
935
+ } catch (error) {
936
+ return {
937
+ success: false,
938
+ message: `Failed to recreate sandbox: ${error.message}`,
939
+ eventId: null
940
+ };
941
+ }
942
+ },
943
+ async regenerateAiCode(src, {
944
+ messageId,
945
+ modelConfig
946
+ }, {
947
+ postService,
948
+ userContext,
949
+ inngestClient
950
+ }) {
951
+ options.logger.trace('(Mutation.regenerateAiCode) messageId [%s], modelConfig [%j]', messageId, modelConfig);
952
+ try {
953
+ const result = await inngestClient.send({
954
+ name: 'regenerate-ai-code',
955
+ data: {
956
+ messageId,
957
+ modelConfig
958
+ }
959
+ });
960
+ return {
961
+ success: true,
962
+ message: 'Code regeneration initiated successfully',
963
+ eventId: result.ids[0]
964
+ };
965
+ } catch (error) {
966
+ options.logger.error('Error regenerating AI code: %o', error);
967
+ throw error;
397
968
  }
398
- return postService.deliverMessage(messageId, userContext.accountId);
399
969
  },
400
- // uploadFile(src, { files, postId }, { fileInfoService, userContext }) {
401
- // const res = [];
402
- // files.map((file) =>
403
- // res.push(
404
- // fileInfoService.create({
405
- // refType: FileRefType.Post as never,
406
- // file,
407
- // createdBy: userContext.accountId,
408
- // ref: postId,
409
- // }),
410
- // ),
411
- // );
412
- // return res;
413
- // },
970
+ async updateSandboxFile(src, {
971
+ projectId,
972
+ messageId,
973
+ filePath,
974
+ content
975
+ }, {
976
+ postService
977
+ }) {
978
+ options.logger.trace('(Mutation.updateSandboxFile) projectId [%s], messageId [%s], filePath [%s], content [%s]', projectId, messageId, filePath, content);
979
+ return postService.updateSandboxFile(projectId, messageId, filePath, content);
980
+ },
414
981
  async TestchatMessage(src, {
415
982
  channelId
416
983
  }, context) {
417
- // if (!context.logger) {
418
- // throw new Error('Logger is not defined in context');
419
- // }
420
- options.pubsub.publish(`CHAT_CREATED.${channelId}`, {
421
- chatMessageTest: {
984
+ options.logger.trace('(Mutation.TestchatMessage) channelId [%s]', channelId);
985
+ try {
986
+ if (!channelId) {
987
+ options.logger.warn('Missing channelId');
988
+ throw new Error('Channel ID is required');
989
+ }
990
+ const testData = {
422
991
  channelId,
423
992
  orgName: 'upadhyaytarun'
424
- }
425
- });
426
- return {
427
- channelId,
428
- orgName: 'upadhyaytarun'
429
- };
993
+ };
994
+ options.logger.debug('Publishing test chat message for channel %s', channelId);
995
+ options.pubsub.publish(`CHAT_CREATED.${channelId}`, {
996
+ chatMessageTest: testData
997
+ });
998
+ return testData;
999
+ } catch (error) {
1000
+ options.logger.error('Error in TestchatMessage: %o', error);
1001
+ throw error;
1002
+ }
430
1003
  }
431
1004
  },
432
1005
  Subscription: {
433
1006
  chatMessageAdded: {
434
- // More on pubsub below
435
1007
  subscribe: (_, {
436
1008
  channelId,
437
1009
  directTo
438
1010
  }) => {
439
- return options.pubsub.asyncIterator([`POST_CREATED.${channelId}`]);
1011
+ options.logger.trace('(Subscription.chatMessageAdded) channelId [%s], directTo [%s]', channelId, directTo);
1012
+ try {
1013
+ if (!channelId) {
1014
+ throw new Error('Channel ID is required for subscription');
1015
+ }
1016
+ return options.pubsub.asyncIterator([`POST_CREATED.${channelId}`]);
1017
+ } catch (error) {
1018
+ options.logger.error('Error subscribing to chatMessageAdded: %o', error);
1019
+ throw error;
1020
+ }
440
1021
  },
441
- resolve: payload => payload
1022
+ resolve: payload => {
1023
+ options.logger.trace('(Subscription.chatMessageAdded.resolve) payload received');
1024
+ return payload;
1025
+ }
442
1026
  },
443
1027
  chatMessageTest: {
444
- // More on pubsub below
445
1028
  subscribe: (_, {
446
1029
  channelId,
447
1030
  directTo
448
1031
  }) => {
449
- return options.pubsub.asyncIterator([`CHAT_CREATED.${channelId}`]);
1032
+ options.logger.trace('(Subscription.chatMessageTest) channelId [%s], directTo [%s]', channelId, directTo);
1033
+ try {
1034
+ if (!channelId) {
1035
+ throw new Error('Channel ID is required for subscription');
1036
+ }
1037
+ return options.pubsub.asyncIterator([`CHAT_CREATED.${channelId}`]);
1038
+ } catch (error) {
1039
+ options.logger.error('Error subscribing to chatMessageTest: %o', error);
1040
+ throw error;
1041
+ }
450
1042
  },
451
- resolve: payload => payload
1043
+ resolve: payload => {
1044
+ options.logger.trace('(Subscription.chatMessageTest.resolve) payload received');
1045
+ return payload;
1046
+ }
452
1047
  },
453
1048
  publicPostAdded: {
454
1049
  subscribe: (_, {
455
1050
  channelId
456
- }) => options.pubsub.asyncIterator([`PUBLIC_POST_CREATED.${channelId}`]),
457
- resolve: payload => payload
1051
+ }) => {
1052
+ options.logger.trace('(Subscription.publicPostAdded) channelId [%s]', channelId);
1053
+ try {
1054
+ if (!channelId) {
1055
+ throw new Error('Channel ID is required for subscription');
1056
+ }
1057
+ return options.pubsub.asyncIterator([`PUBLIC_POST_CREATED.${channelId}`]);
1058
+ } catch (error) {
1059
+ options.logger.error('Error subscribing to publicPostAdded: %o', error);
1060
+ throw error;
1061
+ }
1062
+ },
1063
+ resolve: payload => {
1064
+ options.logger.trace('(Subscription.publicPostAdded.resolve) payload received');
1065
+ return payload;
1066
+ }
1067
+ },
1068
+ sandboxError: {
1069
+ subscribe: withFilter((_, __, context) => context.pubsub.asyncIterator(['SANDBOX_ERROR']), (payload, variables) => payload.sandboxError.projectId === variables.projectId),
1070
+ resolve: (payload, _args, context) => {
1071
+ try {
1072
+ if (!context.userContext) context.userContext = {};
1073
+ if (!context.req) context.req = {};
1074
+ return payload.sandboxError;
1075
+ } catch (error) {
1076
+ options.logger.error('Sandbox error subscription resolve error: %o', error);
1077
+ return payload.sandboxError;
1078
+ }
1079
+ }
1080
+ },
1081
+ fileUpdated: {
1082
+ subscribe: withFilter((_, __, context) => context.pubsub.asyncIterator(['FILE_UPDATED']), (payload, variables) => payload.fileUpdated.projectId === variables.projectId),
1083
+ resolve: (payload, _args, context) => {
1084
+ try {
1085
+ if (!context.userContext) context.userContext = {};
1086
+ if (!context.req) context.req = {};
1087
+ return payload.fileUpdated;
1088
+ } catch (error) {
1089
+ options.logger.error('File update subscription resolve error: %o', error);
1090
+ return payload.fileUpdated;
1091
+ }
1092
+ }
458
1093
  }
459
1094
  }
460
1095
  });export{postResolvers};//# sourceMappingURL=post.js.map