@things-factory/announcement 0.0.0-stage → 10.1.222

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 (122) hide show
  1. package/client/announcement-watch.ts +109 -0
  2. package/client/bootstrap.ts +9 -0
  3. package/client/components/announcement-popup.ts +92 -0
  4. package/client/components/announcement-receipts.ts +196 -0
  5. package/client/components/announcement-sheet.ts +214 -0
  6. package/client/components/audience-picker.ts +221 -0
  7. package/client/index.ts +1 -0
  8. package/client/pages/announcement-editor.ts +505 -0
  9. package/client/pages/announcement-list.ts +200 -0
  10. package/client/pages/announcement-manage-list.ts +113 -0
  11. package/client/pages/announcement-view.ts +172 -0
  12. package/client/route.ts +17 -0
  13. package/client/tsconfig.json +13 -0
  14. package/client/utils/announcement-api.ts +142 -0
  15. package/client/utils/announcement-badge.ts +46 -0
  16. package/client/utils/announcement-words.ts +26 -0
  17. package/dist-client/announcement-watch.d.ts +6 -0
  18. package/dist-client/announcement-watch.js +105 -0
  19. package/dist-client/announcement-watch.js.map +1 -0
  20. package/dist-client/bootstrap.d.ts +1 -0
  21. package/dist-client/bootstrap.js +8 -0
  22. package/dist-client/bootstrap.js.map +1 -0
  23. package/dist-client/components/announcement-popup.d.ts +20 -0
  24. package/dist-client/components/announcement-popup.js +98 -0
  25. package/dist-client/components/announcement-popup.js.map +1 -0
  26. package/dist-client/components/announcement-receipts.d.ts +30 -0
  27. package/dist-client/components/announcement-receipts.js +220 -0
  28. package/dist-client/components/announcement-receipts.js.map +1 -0
  29. package/dist-client/components/announcement-sheet.d.ts +17 -0
  30. package/dist-client/components/announcement-sheet.js +224 -0
  31. package/dist-client/components/announcement-sheet.js.map +1 -0
  32. package/dist-client/components/audience-picker.d.ts +32 -0
  33. package/dist-client/components/audience-picker.js +223 -0
  34. package/dist-client/components/audience-picker.js.map +1 -0
  35. package/dist-client/index.d.ts +1 -0
  36. package/dist-client/index.js +2 -0
  37. package/dist-client/index.js.map +1 -0
  38. package/dist-client/pages/announcement-editor.d.ts +59 -0
  39. package/dist-client/pages/announcement-editor.js +523 -0
  40. package/dist-client/pages/announcement-editor.js.map +1 -0
  41. package/dist-client/pages/announcement-list.d.ts +54 -0
  42. package/dist-client/pages/announcement-list.js +209 -0
  43. package/dist-client/pages/announcement-list.js.map +1 -0
  44. package/dist-client/pages/announcement-manage-list.d.ts +46 -0
  45. package/dist-client/pages/announcement-manage-list.js +115 -0
  46. package/dist-client/pages/announcement-manage-list.js.map +1 -0
  47. package/dist-client/pages/announcement-view.d.ts +30 -0
  48. package/dist-client/pages/announcement-view.js +182 -0
  49. package/dist-client/pages/announcement-view.js.map +1 -0
  50. package/dist-client/route.d.ts +1 -0
  51. package/dist-client/route.js +18 -0
  52. package/dist-client/route.js.map +1 -0
  53. package/dist-client/tsconfig.tsbuildinfo +1 -0
  54. package/dist-client/utils/announcement-api.d.ts +149 -0
  55. package/dist-client/utils/announcement-api.js +52 -0
  56. package/dist-client/utils/announcement-api.js.map +1 -0
  57. package/dist-client/utils/announcement-badge.d.ts +4 -0
  58. package/dist-client/utils/announcement-badge.js +47 -0
  59. package/dist-client/utils/announcement-badge.js.map +1 -0
  60. package/dist-client/utils/announcement-words.d.ts +11 -0
  61. package/dist-client/utils/announcement-words.js +23 -0
  62. package/dist-client/utils/announcement-words.js.map +1 -0
  63. package/dist-server/announcement-read.d.ts +6 -0
  64. package/dist-server/announcement-read.js +45 -0
  65. package/dist-server/announcement-read.js.map +1 -0
  66. package/dist-server/attachment-reader.d.ts +1 -0
  67. package/dist-server/attachment-reader.js +13 -0
  68. package/dist-server/attachment-reader.js.map +1 -0
  69. package/dist-server/index.d.ts +2 -0
  70. package/dist-server/index.js +20 -0
  71. package/dist-server/index.js.map +1 -0
  72. package/dist-server/role-tasks.d.ts +2 -0
  73. package/dist-server/role-tasks.js +13 -0
  74. package/dist-server/role-tasks.js.map +1 -0
  75. package/dist-server/service/announcement/announcement-resolver.d.ts +38 -0
  76. package/dist-server/service/announcement/announcement-resolver.js +664 -0
  77. package/dist-server/service/announcement/announcement-resolver.js.map +1 -0
  78. package/dist-server/service/announcement/announcement-rules.d.ts +150 -0
  79. package/dist-server/service/announcement/announcement-rules.js +227 -0
  80. package/dist-server/service/announcement/announcement-rules.js.map +1 -0
  81. package/dist-server/service/announcement/announcement-service.d.ts +54 -0
  82. package/dist-server/service/announcement/announcement-service.js +248 -0
  83. package/dist-server/service/announcement/announcement-service.js.map +1 -0
  84. package/dist-server/service/announcement/announcement-types.d.ts +54 -0
  85. package/dist-server/service/announcement/announcement-types.js +203 -0
  86. package/dist-server/service/announcement/announcement-types.js.map +1 -0
  87. package/dist-server/service/announcement/announcement.d.ts +124 -0
  88. package/dist-server/service/announcement/announcement.js +462 -0
  89. package/dist-server/service/announcement/announcement.js.map +1 -0
  90. package/dist-server/service/announcement/index.d.ts +4 -0
  91. package/dist-server/service/announcement/index.js +8 -0
  92. package/dist-server/service/announcement/index.js.map +1 -0
  93. package/dist-server/service/index.d.ts +7 -0
  94. package/dist-server/service/index.js +23 -0
  95. package/dist-server/service/index.js.map +1 -0
  96. package/dist-server/tsconfig.tsbuildinfo +1 -0
  97. package/package.json +51 -4
  98. package/server/announcement-read.ts +44 -0
  99. package/server/attachment-reader.ts +11 -0
  100. package/server/index.ts +16 -0
  101. package/server/role-tasks.ts +12 -0
  102. package/server/service/announcement/announcement-resolver.ts +506 -0
  103. package/server/service/announcement/announcement-rules.ts +239 -0
  104. package/server/service/announcement/announcement-service.ts +240 -0
  105. package/server/service/announcement/announcement-types.ts +138 -0
  106. package/server/service/announcement/announcement.ts +367 -0
  107. package/server/service/announcement/index.ts +5 -0
  108. package/server/service/index.ts +19 -0
  109. package/server/tsconfig.json +11 -0
  110. package/test/announcement-persistence.test.ts +271 -0
  111. package/test/announcement-read.test.ts +49 -0
  112. package/test/announcement-rules.test.ts +217 -0
  113. package/test/open-doors.test.ts +45 -0
  114. package/test/translations.test.ts +54 -0
  115. package/things-factory.config.js +13 -0
  116. package/translations/en.json +169 -0
  117. package/translations/es.json +169 -0
  118. package/translations/ja.json +169 -0
  119. package/translations/ko.json +169 -0
  120. package/translations/ms.json +169 -0
  121. package/translations/zh.json +169 -0
  122. package/README.md +0 -3
@@ -0,0 +1,367 @@
1
+ /*
2
+ * The tables of announcements (ADR-0163 decisions 1-4): an announcement, whom it is addressed to, each person's reading
3
+ * of it, and the versions it went through after it was published. Whom it reaches and who read it are kept here -
4
+ * the inbox, web push and the popup only tell of it.
5
+ */
6
+ import { Column, CreateDateColumn, DeleteDateColumn, Entity, Index, ManyToOne, PrimaryGeneratedColumn, RelationId, UpdateDateColumn } from 'typeorm'
7
+ import { Field, ID, Int, ObjectType, registerEnumType } from 'type-graphql'
8
+ import { GraphQLJSON } from 'graphql-scalars'
9
+
10
+ import { Domain } from '@things-factory/shell'
11
+ import { User } from '@things-factory/auth-base'
12
+
13
+ /** Where an announcement stands. An announcement past its end is still PUBLISHED - it is a past one, read from the list of past ones. */
14
+ export enum AnnouncementStatus {
15
+ /** Being written - only its writer sees it. */
16
+ DRAFT = 'DRAFT',
17
+ /** Published to start later - published and told when its start comes. */
18
+ SCHEDULED = 'SCHEDULED',
19
+ /** Out to its audience. */
20
+ PUBLISHED = 'PUBLISHED',
21
+ /** Taken back, with the reason - no longer shown to its audience. */
22
+ WITHDRAWN = 'WITHDRAWN'
23
+ }
24
+
25
+ registerEnumType(AnnouncementStatus, { name: 'AnnouncementStatus', description: 'Where an announcement stands: draft, scheduled, published or withdrawn.' })
26
+
27
+ /** How much an announcement asks of the reader's attention - the list puts the urgent and important ones first. */
28
+ export enum AnnouncementImportance {
29
+ NORMAL = 'NORMAL',
30
+ IMPORTANT = 'IMPORTANT',
31
+ URGENT = 'URGENT'
32
+ }
33
+
34
+ registerEnumType(AnnouncementImportance, { name: 'AnnouncementImportance', description: 'How much an announcement asks of the reader: normal, important or urgent.' })
35
+
36
+ @Entity()
37
+ @Index('ix_announcement_0', (one: Announcement) => [one.domain, one.status, one.startsAt])
38
+ @ObjectType({ description: 'An announcement of a domain - written once, addressed to an audience, read and acknowledged by each of its people.' })
39
+ export class Announcement {
40
+ @PrimaryGeneratedColumn('uuid')
41
+ @Field(type => ID, { description: 'Announcement id.' })
42
+ readonly id: string
43
+
44
+ @ManyToOne(type => Domain)
45
+ @Field(type => Domain, { nullable: true, description: 'The domain the announcement is in.' })
46
+ domain?: Domain
47
+
48
+ @RelationId((one: Announcement) => one.domain)
49
+ domainId?: string
50
+
51
+ @Column()
52
+ @Field({ description: 'The title.' })
53
+ title: string
54
+
55
+ @Column({ type: 'text', nullable: true })
56
+ @Field({ nullable: true, description: 'The body, in Markdown.' })
57
+ body?: string
58
+
59
+ @Column({ default: 'GENERAL' })
60
+ @Field({ description: 'The kind of announcement - one of the codes this module declares.' })
61
+ category: string
62
+
63
+ @Column({ default: AnnouncementImportance.NORMAL })
64
+ @Field(type => AnnouncementImportance, { description: 'Normal, important or urgent.' })
65
+ importance: AnnouncementImportance
66
+
67
+ @Column({ default: AnnouncementStatus.DRAFT })
68
+ @Field(type => AnnouncementStatus, { description: 'Draft, scheduled, published or withdrawn.' })
69
+ status: AnnouncementStatus
70
+
71
+ @Column({ nullable: true })
72
+ @Field({ nullable: true, description: 'When it starts to be shown - empty publishes it at once.' })
73
+ startsAt?: Date
74
+
75
+ @Column({ nullable: true })
76
+ @Field({ nullable: true, description: 'When it stops being a current one - empty keeps it current.' })
77
+ endsAt?: Date
78
+
79
+ @Column({ default: false })
80
+ @Field({ description: 'Kept on top of the list while it is current.' })
81
+ pinned: boolean
82
+
83
+ @Column('int', { default: 12 })
84
+ @Field(type => Int, { description: "Its place in the readers' list - pinned first, then urgent, important and normal (announcement-rules rankOf)." })
85
+ rank: number
86
+
87
+ @Column({ default: false })
88
+ @Field({ description: 'Each reader is asked to acknowledge it, apart from reading it.' })
89
+ requiresAcknowledgement: boolean
90
+
91
+ @Column({ nullable: true })
92
+ @Field({ nullable: true, description: 'The day by which the readers are asked to acknowledge it.' })
93
+ acknowledgeBy?: Date
94
+
95
+ @Column({ default: false })
96
+ @Field({ description: 'Shown in a popup to its readers - once, or until acknowledged when it asks for that.' })
97
+ popup: boolean
98
+
99
+ @Column({ default: false })
100
+ @Field({ description: 'Sent by mail too - where the domain has turned mail on (notification Notice::email).' })
101
+ sendEmail: boolean
102
+
103
+ @Column('simple-json', { nullable: true })
104
+ @Field(type => GraphQLJSON, {
105
+ nullable: true,
106
+ description: 'Each time its people were told - [{ at, kind: published | changed | reminder, receivers, email: { sent, failed }, teams: { sent, failed } }], the newest last.'
107
+ })
108
+ deliveries?: { at: string; kind: string; receivers: number; email: { sent: number; failed: number }; teams: { sent: number; failed: number }; failed?: boolean }[]
109
+
110
+ @Column('simple-json', { nullable: true })
111
+ @Field(type => GraphQLJSON, { nullable: true, description: 'Screens it points to - [{ label, url }], a screen of the application or an https address.' })
112
+ links?: { label: string; url: string }[]
113
+
114
+ @Column('int', { default: 1 })
115
+ @Field(type => Int, { description: 'The version - one more each time it is changed after it was published.' })
116
+ version: number
117
+
118
+ @Column({ nullable: true })
119
+ @Field({ nullable: true, description: 'When it went out to its audience.' })
120
+ publishedAt?: Date
121
+
122
+ @ManyToOne(type => User, { nullable: true })
123
+ @Field(type => User, { nullable: true, description: 'Who published it.' })
124
+ publisher?: User
125
+
126
+ @RelationId((one: Announcement) => one.publisher)
127
+ publisherId?: string
128
+
129
+ @Column('int', { nullable: true })
130
+ @Field(type => Int, { nullable: true, description: 'How many people it was addressed to - counted when it went out, and again when its audience changed.' })
131
+ audienceSize?: number
132
+
133
+ @Column({ nullable: true })
134
+ @Field({ nullable: true, description: 'When it was withdrawn.' })
135
+ withdrawnAt?: Date
136
+
137
+ @Column({ nullable: true })
138
+ @Field({ nullable: true, description: 'Why it was withdrawn.' })
139
+ withdrawReason?: string
140
+
141
+ @ManyToOne(type => User, { nullable: true })
142
+ @Field(type => User, { nullable: true, description: 'Who wrote it.' })
143
+ creator?: User
144
+
145
+ @RelationId((one: Announcement) => one.creator)
146
+ creatorId?: string
147
+
148
+ @ManyToOne(type => User, { nullable: true })
149
+ @Field(type => User, { nullable: true, description: 'Who changed it last.' })
150
+ updater?: User
151
+
152
+ @RelationId((one: Announcement) => one.updater)
153
+ updaterId?: string
154
+
155
+ @CreateDateColumn()
156
+ @Field({ nullable: true, description: 'Creation timestamp.' })
157
+ createdAt?: Date
158
+
159
+ @UpdateDateColumn()
160
+ @Field({ nullable: true, description: 'Last update timestamp.' })
161
+ updatedAt?: Date
162
+
163
+ @DeleteDateColumn()
164
+ @Field({ nullable: true, description: 'Soft delete timestamp.' })
165
+ deletedAt?: Date
166
+ }
167
+
168
+ @Entity()
169
+ @Index('ix_announcement_audience_0', (one: AnnouncementAudience) => [one.announcement, one.scope, one.targetId], { unique: true, where: '"deleted_at" IS NULL' })
170
+ @Index('ix_announcement_audience_1', (one: AnnouncementAudience) => [one.domain, one.scope, one.targetId])
171
+ @ObjectType({ description: 'One target of an announcement - the domain, a department with those under it, a role or a person (organization audience).' })
172
+ export class AnnouncementAudience {
173
+ @PrimaryGeneratedColumn('uuid')
174
+ @Field(type => ID, { description: 'Target id.' })
175
+ readonly id: string
176
+
177
+ @ManyToOne(type => Domain)
178
+ @Field(type => Domain, { nullable: true, description: 'The domain.' })
179
+ domain?: Domain
180
+
181
+ @RelationId((one: AnnouncementAudience) => one.domain)
182
+ domainId?: string
183
+
184
+ @ManyToOne(type => Announcement, { onDelete: 'CASCADE' })
185
+ announcement?: Announcement
186
+
187
+ @RelationId((one: AnnouncementAudience) => one.announcement)
188
+ @Field({ description: 'The announcement.' })
189
+ announcementId?: string
190
+
191
+ @Column()
192
+ @Field({ description: 'DOMAIN, DEPARTMENT, ROLE or USER.' })
193
+ scope: string
194
+
195
+ @Column()
196
+ @Field({ description: 'The domain, department, role or user id.' })
197
+ targetId: string
198
+
199
+ @Column({ nullable: true })
200
+ @Field({ nullable: true, description: 'Its name when it was given - shown to the writer.' })
201
+ targetName?: string
202
+
203
+ @ManyToOne(type => User, { nullable: true })
204
+ @Field(type => User, { nullable: true, description: 'Who gave it.' })
205
+ creator?: User
206
+
207
+ @RelationId((one: AnnouncementAudience) => one.creator)
208
+ creatorId?: string
209
+
210
+ @ManyToOne(type => User, { nullable: true })
211
+ @Field(type => User, { nullable: true, description: 'Who changed it last.' })
212
+ updater?: User
213
+
214
+ @RelationId((one: AnnouncementAudience) => one.updater)
215
+ updaterId?: string
216
+
217
+ @CreateDateColumn()
218
+ @Field({ nullable: true, description: 'Creation timestamp.' })
219
+ createdAt?: Date
220
+
221
+ @UpdateDateColumn()
222
+ @Field({ nullable: true, description: 'Last update timestamp.' })
223
+ updatedAt?: Date
224
+
225
+ @DeleteDateColumn()
226
+ @Field({ nullable: true, description: 'When the target was taken off.' })
227
+ deletedAt?: Date
228
+ }
229
+
230
+ @Entity()
231
+ @Index('ix_announcement_receipt_0', (one: AnnouncementReceipt) => [one.announcement, one.user], { unique: true, where: '"deleted_at" IS NULL' })
232
+ @Index('ix_announcement_receipt_1', (one: AnnouncementReceipt) => [one.domain, one.user])
233
+ @ObjectType({ description: "One person's reading of an announcement - whether they were addressed when it went out, when they read it and when they acknowledged it." })
234
+ export class AnnouncementReceipt {
235
+ @PrimaryGeneratedColumn('uuid')
236
+ @Field(type => ID, { description: 'Receipt id.' })
237
+ readonly id: string
238
+
239
+ @ManyToOne(type => Domain)
240
+ @Field(type => Domain, { nullable: true, description: 'The domain.' })
241
+ domain?: Domain
242
+
243
+ @RelationId((one: AnnouncementReceipt) => one.domain)
244
+ domainId?: string
245
+
246
+ @ManyToOne(type => Announcement, { onDelete: 'CASCADE' })
247
+ announcement?: Announcement
248
+
249
+ @RelationId((one: AnnouncementReceipt) => one.announcement)
250
+ @Field({ description: 'The announcement.' })
251
+ announcementId?: string
252
+
253
+ @ManyToOne(type => User)
254
+ @Field(type => User, { nullable: true, description: 'The reader.' })
255
+ user?: User
256
+
257
+ @RelationId((one: AnnouncementReceipt) => one.user)
258
+ @Field({ description: 'The reader user id.' })
259
+ userId?: string
260
+
261
+ @Column({ default: false })
262
+ @Field({ description: 'Whether the person is on its roster - addressed when it went out or when its audience changed; one who came to it later is not.' })
263
+ listed: boolean
264
+
265
+ @Column({ nullable: true })
266
+ @Field({ nullable: true, description: 'When the person was last told of it.' })
267
+ notifiedAt?: Date
268
+
269
+ @Column({ nullable: true })
270
+ @Field({ nullable: true, description: 'When the person first read it.' })
271
+ readAt?: Date
272
+
273
+ @Column({ nullable: true })
274
+ @Field({ nullable: true, description: 'When the person acknowledged it.' })
275
+ acknowledgedAt?: Date
276
+
277
+ @ManyToOne(type => User, { nullable: true })
278
+ @Field(type => User, { nullable: true, description: 'Who made the row - the publisher for the roster, the reader otherwise.' })
279
+ creator?: User
280
+
281
+ @RelationId((one: AnnouncementReceipt) => one.creator)
282
+ creatorId?: string
283
+
284
+ @ManyToOne(type => User, { nullable: true })
285
+ @Field(type => User, { nullable: true, description: 'Who changed it last.' })
286
+ updater?: User
287
+
288
+ @RelationId((one: AnnouncementReceipt) => one.updater)
289
+ updaterId?: string
290
+
291
+ @CreateDateColumn()
292
+ @Field({ nullable: true, description: 'Creation timestamp.' })
293
+ createdAt?: Date
294
+
295
+ @UpdateDateColumn()
296
+ @Field({ nullable: true, description: 'Last update timestamp.' })
297
+ updatedAt?: Date
298
+
299
+ @DeleteDateColumn()
300
+ @Field({ nullable: true, description: 'Soft delete timestamp.' })
301
+ deletedAt?: Date
302
+ }
303
+
304
+ @Entity()
305
+ @Index('ix_announcement_revision_0', (one: AnnouncementRevision) => [one.announcement, one.version], { unique: true, where: '"deleted_at" IS NULL' })
306
+ @ObjectType({ description: 'One version of a published announcement - what it said and whom it was addressed to.' })
307
+ export class AnnouncementRevision {
308
+ @PrimaryGeneratedColumn('uuid')
309
+ @Field(type => ID, { description: 'Revision id.' })
310
+ readonly id: string
311
+
312
+ @ManyToOne(type => Domain)
313
+ @Field(type => Domain, { nullable: true, description: 'The domain.' })
314
+ domain?: Domain
315
+
316
+ @RelationId((one: AnnouncementRevision) => one.domain)
317
+ domainId?: string
318
+
319
+ @ManyToOne(type => Announcement, { onDelete: 'CASCADE' })
320
+ announcement?: Announcement
321
+
322
+ @RelationId((one: AnnouncementRevision) => one.announcement)
323
+ @Field({ description: 'The announcement.' })
324
+ announcementId?: string
325
+
326
+ @Column('int')
327
+ @Field(type => Int, { description: 'The version.' })
328
+ version: number
329
+
330
+ @Column()
331
+ @Field({ description: 'The title of this version.' })
332
+ title: string
333
+
334
+ @Column({ type: 'text', nullable: true })
335
+ @Field({ nullable: true, description: 'The body of this version.' })
336
+ body?: string
337
+
338
+ @Column('simple-json', { nullable: true })
339
+ @Field(type => GraphQLJSON, { nullable: true, description: 'The rest of this version - kind, importance, period, flags, links and audience.' })
340
+ rest?: Record<string, unknown>
341
+
342
+ @ManyToOne(type => User, { nullable: true })
343
+ @Field(type => User, { nullable: true, description: 'Who made this version.' })
344
+ creator?: User
345
+
346
+ @RelationId((one: AnnouncementRevision) => one.creator)
347
+ creatorId?: string
348
+
349
+ @ManyToOne(type => User, { nullable: true })
350
+ @Field(type => User, { nullable: true, description: 'Who changed it last.' })
351
+ updater?: User
352
+
353
+ @RelationId((one: AnnouncementRevision) => one.updater)
354
+ updaterId?: string
355
+
356
+ @CreateDateColumn()
357
+ @Field({ nullable: true, description: 'Creation timestamp.' })
358
+ createdAt?: Date
359
+
360
+ @UpdateDateColumn()
361
+ @Field({ nullable: true, description: 'Last update timestamp.' })
362
+ updatedAt?: Date
363
+
364
+ @DeleteDateColumn()
365
+ @Field({ nullable: true, description: 'Soft delete timestamp.' })
366
+ deletedAt?: Date
367
+ }
@@ -0,0 +1,5 @@
1
+ import { Announcement, AnnouncementAudience, AnnouncementReceipt, AnnouncementRevision } from './announcement'
2
+ import { AnnouncementResolver } from './announcement-resolver'
3
+
4
+ export const entities = [Announcement, AnnouncementAudience, AnnouncementReceipt, AnnouncementRevision]
5
+ export const resolvers = [AnnouncementResolver]
@@ -0,0 +1,19 @@
1
+ /* EXPORT ENTITY TYPES */
2
+ export * from './announcement/announcement'
3
+ export * from './announcement/announcement-rules'
4
+ export { ANNOUNCEMENT_REF_TYPE, visibleAnnouncement } from './announcement/announcement-service'
5
+
6
+ /* IMPORT ENTITIES AND RESOLVERS */
7
+ import { entities as AnnouncementEntities, resolvers as AnnouncementResolvers } from './announcement'
8
+
9
+ export const entities = [
10
+ /* ENTITIES */
11
+ ...AnnouncementEntities
12
+ ]
13
+
14
+ export const schema = {
15
+ resolverClasses: [
16
+ /* RESOLVER CLASSES */
17
+ ...AnnouncementResolvers
18
+ ]
19
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig-base.json",
3
+ "compilerOptions": {
4
+ "strict": false,
5
+ "module": "commonjs",
6
+ "outDir": "../dist-server",
7
+ "baseUrl": "./"
8
+ },
9
+ "include": ["./**/*"],
10
+ "exclude": ["**/*.spec.ts", "**/*.test.ts"]
11
+ }
@@ -0,0 +1,271 @@
1
+ /**
2
+ * Who an announcement reaches and who is on its roster, on sqlite with the real entities (ADR-0163 decisions 2-4):
3
+ * the organization audience (a department reaches those under it, active records only, roles, the domain), who may see
4
+ * one, sending it out, changing its audience after, and publishing a scheduled one exactly once.
5
+ *
6
+ * Reads the built server (`dist-server`) - type-graphql needs tsc's decorator metadata.
7
+ */
8
+ import 'reflect-metadata'
9
+
10
+ const { existsSync } = require('fs')
11
+ const { join } = require('path')
12
+ const { DataSource } = require('typeorm')
13
+
14
+ /* What notification was asked to send (ADR-0164) - the channels themselves are notification's tests. */
15
+ const told: { domain: any; notice: any }[] = []
16
+ jest.mock('@things-factory/notification', () => ({
17
+ sendNotice: async (domain: any, notice: any) => {
18
+ told.push({ domain, notice })
19
+ return { receivers: notice.receivers.length, email: { sent: notice.channels?.email ? notice.receivers.length : 0, failed: 0 }, teams: { sent: 0, failed: 0 } }
20
+ },
21
+ sendNoticeWhenCommitted: () => {},
22
+ noticeLinkOf: () => undefined
23
+ }))
24
+
25
+ const shell = require('@things-factory/shell')
26
+ const authBase = require('@things-factory/auth-base')
27
+ const contact = require('@things-factory/contact')
28
+ const bizBase = require('@things-factory/biz-base')
29
+ const attachmentBase = require('@things-factory/attachment-base')
30
+ const organization = require('@things-factory/organization')
31
+
32
+ function loadCompiled(modulePath: string): any {
33
+ const resolved = join(__dirname, '..', 'dist-server', `${modulePath}.js`)
34
+ if (!existsSync(resolved)) throw new Error(`announcement build output is missing: ${resolved}\nBuild it first: npm run build:server`)
35
+ return require(resolved)
36
+ }
37
+
38
+ const service = loadCompiled('service/announcement/announcement-service')
39
+ const { AnnouncementResolver } = loadCompiled('service/announcement/announcement-resolver')
40
+ const { Announcement, AnnouncementAudience, AnnouncementReceipt, AnnouncementRevision } = loadCompiled('service/announcement/announcement')
41
+
42
+ let dataSource: any
43
+ let domain: any
44
+ let people: Record<string, any>
45
+ let departments: Record<string, any>
46
+ let hr: any
47
+
48
+ beforeAll(async () => {
49
+ dataSource = new DataSource({
50
+ type: 'sqlite',
51
+ database: ':memory:',
52
+ synchronize: true,
53
+ dropSchema: true,
54
+ namingStrategy: new shell.NamingStrategy(),
55
+ entities: [...shell.entities, ...authBase.entities, ...bizBase.entities, ...contact.entities, ...organization.entities, ...attachmentBase.entities, Announcement, AnnouncementAudience, AnnouncementReceipt, AnnouncementRevision],
56
+ logging: false
57
+ })
58
+ await dataSource.initialize()
59
+ shell.addDataSource('default', dataSource)
60
+ shell.addDataSource('tx', dataSource)
61
+ })
62
+
63
+ afterAll(async () => {
64
+ if (!dataSource) return
65
+ shell.removeDataSource('default')
66
+ shell.removeDataSource('tx')
67
+ await dataSource.destroy()
68
+ })
69
+
70
+ /*
71
+ * 본부(hq) > 영업팀(sales) > 영업1파트(sales-1), and 경영지원(admin) on its own.
72
+ * writer - 경영지원, holds HR · part - 영업1파트 · team - 영업팀 · left - 영업1파트 but inactive · outsider - not a member.
73
+ */
74
+ beforeEach(async () => {
75
+ told.length = 0
76
+ await dataSource.synchronize(true)
77
+ domain = await dataSource.getRepository(shell.Domain).save({ name: 'announce', subdomain: 'announce' })
78
+ const other = await dataSource.getRepository(shell.Domain).save({ name: 'elsewhere', subdomain: 'elsewhere' })
79
+ hr = await dataSource.getRepository(authBase.Role).save({ name: 'HR', domain })
80
+
81
+ const user = (name: string, domains: any[], roles: any[] = []) =>
82
+ dataSource.getRepository(authBase.User).save({ name, email: `${name}@announce.test`, status: 'activated', domains, roles })
83
+ people = {
84
+ writer: await user('writer', [domain], [hr]),
85
+ part: await user('part', [domain]),
86
+ team: await user('team', [domain]),
87
+ left: await user('left', [domain]),
88
+ outsider: await user('outsider', [other])
89
+ }
90
+
91
+ const department = (name: string, parent?: any) => dataSource.getRepository(organization.Department).save({ domain, controlNo: name, name, parent })
92
+ departments = {}
93
+ departments.hq = await department('hq')
94
+ departments.sales = await department('sales', departments.hq)
95
+ departments['sales-1'] = await department('sales-1', departments.sales)
96
+ departments.admin = await department('admin')
97
+
98
+ const employee = (who: string, where: string, active = true) =>
99
+ dataSource.getRepository(organization.Employee).save({ domain, controlNo: who, name: who, user: people[who], department: departments[where], active })
100
+ await employee('writer', 'admin')
101
+ await employee('part', 'sales-1')
102
+ await employee('team', 'sales')
103
+ await employee('left', 'sales-1', false)
104
+ })
105
+
106
+ const ids = (...names: string[]) => names.map(name => people[name].id).sort()
107
+ const contextOf = (who: string, writer = false) => ({ state: { domain, user: people[who], __announcementWriter: Promise.resolve(writer) } })
108
+
109
+ async function draft(audiences: { scope: string; targetId: string }[], extra: any = {}) {
110
+ const one = await dataSource.getRepository(Announcement).save({ domain, title: '연말 휴무', body: '**12월 31일** 휴무', status: 'DRAFT', creator: people.writer, ...extra })
111
+ for (const target of audiences) await dataSource.getRepository(AnnouncementAudience).save({ domain, announcement: one, ...target })
112
+ return one
113
+ }
114
+
115
+ describe('whom an audience names (organization)', () => {
116
+ it('★ a department names the active people of it and of every department under it', async () => {
117
+ expect((await organization.audienceUserIds(domain, [{ scope: 'DEPARTMENT', targetId: departments.hq.id }])).sort()).toEqual(ids('part', 'team'))
118
+ expect((await organization.audienceUserIds(domain, [{ scope: 'DEPARTMENT', targetId: departments['sales-1'].id }])).sort()).toEqual(ids('part'))
119
+ })
120
+
121
+ it('★ the domain names its members, a role its holders, a person only a member', async () => {
122
+ expect((await organization.audienceUserIds(domain, [{ scope: 'DOMAIN', targetId: domain.id }])).sort()).toEqual(ids('writer', 'part', 'team', 'left'))
123
+ expect(await organization.audienceUserIds(domain, [{ scope: 'ROLE', targetId: hr.id }])).toEqual(ids('writer'))
124
+ expect(await organization.audienceUserIds(domain, [{ scope: 'USER', targetId: people.outsider.id }])).toEqual([])
125
+ })
126
+
127
+ it('★ a person stands in their department and every one above it, with their roles', async () => {
128
+ const member = await organization.audienceMemberOf(domain, people.part)
129
+ expect([...member.departmentIds].sort()).toEqual([departments.hq.id, departments.sales.id, departments['sales-1'].id].sort())
130
+ expect(member.ownDepartments).toEqual([{ id: departments['sales-1'].id, name: 'sales-1' }])
131
+ expect((await organization.audienceMemberOf(domain, people.writer)).roleIds).toEqual([hr.id])
132
+ expect((await organization.audienceMemberOf(domain, people.left)).departmentIds).toEqual([])
133
+ })
134
+ })
135
+
136
+ describe('who may see an announcement', () => {
137
+ it('★ its audience once it is out - not before, and nobody else', async () => {
138
+ const one = await draft([{ scope: 'DEPARTMENT', targetId: departments.sales.id }])
139
+ expect(await service.visibleAnnouncement(one.id, contextOf('part'))).toBeUndefined()
140
+ await dataSource.getRepository(Announcement).update(one.id, { status: 'PUBLISHED' })
141
+ expect(await service.visibleAnnouncement(one.id, contextOf('part'))).toMatchObject({ reader: true, writer: false })
142
+ expect(await service.visibleAnnouncement(one.id, contextOf('team'))).toMatchObject({ reader: true })
143
+ expect(await service.visibleAnnouncement(one.id, contextOf('left'))).toBeUndefined()
144
+ expect(await service.visibleAnnouncement(one.id, contextOf('writer'))).toBeUndefined()
145
+ expect(await service.visibleAnnouncement(one.id, { state: { domain, user: people.outsider } })).toBeUndefined()
146
+ })
147
+
148
+ it("★ a writer sees every one but somebody else's draft", async () => {
149
+ const mine = await draft([{ scope: 'USER', targetId: people.part.id }])
150
+ expect(await service.visibleAnnouncement(mine.id, contextOf('writer', true))).toMatchObject({ reader: false, writer: true })
151
+ expect(await service.visibleAnnouncement(mine.id, contextOf('team', true))).toBeUndefined()
152
+ await dataSource.getRepository(Announcement).update(mine.id, { status: 'WITHDRAWN' })
153
+ expect(await service.visibleAnnouncement(mine.id, contextOf('team', true))).toMatchObject({ writer: true })
154
+ expect(await service.visibleAnnouncement(mine.id, contextOf('part'))).toBeUndefined()
155
+ })
156
+
157
+ it('a withdrawn one is no longer its audience\'s', async () => {
158
+ const one = await draft([{ scope: 'DOMAIN', targetId: domain.id }], { status: 'WITHDRAWN' })
159
+ expect(await service.visibleAnnouncement(one.id, contextOf('part'))).toBeUndefined()
160
+ })
161
+ })
162
+
163
+ describe('sending it out', () => {
164
+ it('★ publishes it, lists its roster, keeps the version, and tells everyone but its publisher', async () => {
165
+ const one = await draft([
166
+ { scope: 'DEPARTMENT', targetId: departments.sales.id },
167
+ { scope: 'ROLE', targetId: hr.id }
168
+ ])
169
+ const receivers = await dataSource.transaction((tx: any) => service.sendOut(one, domain, people.writer, tx))
170
+ expect([...receivers].sort()).toEqual(ids('part', 'team'))
171
+ const fresh = await dataSource.getRepository(Announcement).findOneBy({ id: one.id })
172
+ expect(fresh).toMatchObject({ status: 'PUBLISHED', audienceSize: 3, publisherId: people.writer.id })
173
+ expect(fresh.publishedAt).toBeTruthy()
174
+ expect(fresh.startsAt).toBeTruthy()
175
+ const roster = await dataSource.getRepository(AnnouncementReceipt).find({ where: { announcement: { id: one.id } } })
176
+ expect(roster.filter((r: any) => r.listed).map((r: any) => r.userId).sort()).toEqual(ids('writer', 'part', 'team'))
177
+ const revisions = await dataSource.getRepository(AnnouncementRevision).find({ where: { announcement: { id: one.id } } })
178
+ expect(revisions.map((r: any) => r.version)).toEqual([1])
179
+ expect(revisions[0].rest.audiences).toHaveLength(2)
180
+ })
181
+
182
+ it('★ a changed audience lists the newly named, unlists those no longer named and keeps their reading', async () => {
183
+ const one = await draft([{ scope: 'DEPARTMENT', targetId: departments['sales-1'].id }])
184
+ await dataSource.transaction((tx: any) => service.sendOut(one, domain, people.writer, tx))
185
+ await dataSource.getRepository(AnnouncementReceipt).update({ announcement: { id: one.id }, user: { id: people.part.id } }, { readAt: new Date() })
186
+
187
+ await dataSource.getRepository(AnnouncementAudience).delete({ announcement: { id: one.id } })
188
+ await dataSource.getRepository(AnnouncementAudience).save({ domain, announcement: one, scope: 'USER', targetId: people.team.id })
189
+ const added = await dataSource.transaction((tx: any) => service.alignRoster(one, domain, people.writer, tx))
190
+
191
+ expect(added).toEqual(ids('team'))
192
+ const roster = await dataSource.getRepository(AnnouncementReceipt).find({ where: { announcement: { id: one.id } } })
193
+ const part = roster.find((r: any) => r.userId === people.part.id)
194
+ expect(part).toMatchObject({ listed: false })
195
+ expect(part.readAt).toBeTruthy()
196
+ expect((await dataSource.getRepository(Announcement).findOneBy({ id: one.id })).audienceSize).toBe(1)
197
+ })
198
+ })
199
+
200
+ describe('a scheduled announcement', () => {
201
+ it('★ goes out at its start, once, its roster is told through notification and what went out is kept', async () => {
202
+ const one = await draft([{ scope: 'DOMAIN', targetId: domain.id }], { status: 'SCHEDULED', startsAt: new Date(Date.now() - 1000), publisher: people.writer, sendEmail: true })
203
+ const later = await draft([{ scope: 'DOMAIN', targetId: domain.id }], { status: 'SCHEDULED', startsAt: new Date(Date.now() + 3600 * 1000) })
204
+
205
+ /* sqlite has one connection, so two servers' runs are played one after the other; the second finds it taken. */
206
+ expect(await service.publishDue()).toBe(1)
207
+ expect(await service.publishDue()).toBe(0)
208
+ expect((await dataSource.getRepository(Announcement).findOneBy({ id: one.id })).status).toBe('PUBLISHED')
209
+ expect((await dataSource.getRepository(Announcement).findOneBy({ id: later.id })).status).toBe('SCHEDULED')
210
+
211
+ /* Told through notification, once, to all but its publisher - by mail only because it asked for it - and kept. */
212
+ expect(told).toHaveLength(1)
213
+ const { notice } = told[0]
214
+ expect(notice).toMatchObject({ type: 'announcement', address: `announcement-view?id=${one.id}`, format: 'markdown', channels: { email: true }, broadcast: true })
215
+ expect(notice.receivers.map((r: any) => r.id).sort()).toEqual(ids('part', 'team', 'left'))
216
+ expect(notice.receivers.find((r: any) => r.id === people.part.id)).toMatchObject({ email: 'part@announce.test' })
217
+ expect(notice.words('ko')).toMatchObject({ subject: '연말 휴무', summary: '12월 31일 휴무', body: '**12월 31일** 휴무' })
218
+ const kept = (await dataSource.getRepository(Announcement).findOneBy({ id: one.id })).deliveries
219
+ expect(kept).toEqual([expect.objectContaining({ kind: 'published', receivers: 3, email: { sent: 3, failed: 0 } })])
220
+ })
221
+ })
222
+
223
+ describe('the channel everyone reads', () => {
224
+ it('★ a scheduled announcement for one department is told to its people, never to the channel', async () => {
225
+ await draft([{ scope: 'DEPARTMENT', targetId: departments.sales.id }], { status: 'SCHEDULED', startsAt: new Date(Date.now() - 1000), publisher: people.writer })
226
+ expect(await service.publishDue()).toBe(1)
227
+ expect(told).toHaveLength(1)
228
+ expect(told[0].notice.broadcast).toBeUndefined()
229
+ expect(told[0].notice.receivers.map((r: any) => r.id).sort()).toEqual(ids('part', 'team'))
230
+ })
231
+ })
232
+
233
+ describe("the reader's list - what the screen and the assistant read", () => {
234
+ const list = (who: string, filters: any[] = []) => new AnnouncementResolver().announcements({ filters, pagination: { page: 1, limit: 50 } }, contextOf(who))
235
+ const titles = async (who: string, filters: any[] = []) => (await list(who, filters)).items.map((one: any) => one.title).sort()
236
+
237
+ beforeEach(async () => {
238
+ const out = async (title: string, extra: any = {}) => {
239
+ const one = await draft([{ scope: 'DOMAIN', targetId: domain.id }], { title, status: 'PUBLISHED', publishedAt: new Date(), ...extra })
240
+ return one
241
+ }
242
+ const read = await out('read')
243
+ await out('unread')
244
+ const asks = await out('asks', { requiresAcknowledgement: true })
245
+ await out('past', { endsAt: new Date(Date.now() - 1000) })
246
+ await draft([{ scope: 'DOMAIN', targetId: domain.id }], { title: 'draft' })
247
+ const receipt = (one: any, extra: any) => dataSource.getRepository(AnnouncementReceipt).save({ domain, announcement: one, user: people.part, listed: true, ...extra })
248
+ await receipt(read, { readAt: new Date() })
249
+ await receipt(asks, { readAt: new Date() })
250
+ })
251
+
252
+ it('★ every one out that names the person, current and past, never a draft', async () => {
253
+ expect(await titles('part')).toEqual(['asks', 'past', 'read', 'unread'])
254
+ })
255
+
256
+ it('★ the period narrows to the current or the past ones', async () => {
257
+ expect(await titles('part', [{ name: 'period', operator: 'eq', value: 'current' }])).toEqual(['asks', 'read', 'unread'])
258
+ expect(await titles('part', [{ name: 'period', operator: 'eq', value: 'past' }])).toEqual(['past'])
259
+ })
260
+
261
+ it("★ the reader's state narrows by this reader's own receipts", async () => {
262
+ expect(await titles('part', [{ name: 'readerState', operator: 'in', value: ['unread', 'to-acknowledge'] }])).toEqual(['asks', 'past', 'unread'])
263
+ expect(await titles('part', [{ name: 'readerState', operator: 'eq', value: 'read' }])).toEqual(['read'])
264
+ /* Another person has read nothing - the same filter answers for them alone. */
265
+ expect(await titles('team', [{ name: 'readerState', operator: 'eq', value: 'read' }])).toEqual([])
266
+ })
267
+
268
+ it('an unknown state is refused', async () => {
269
+ await expect(list('part', [{ name: 'readerState', operator: 'eq', value: 'lost' }])).rejects.toThrow()
270
+ })
271
+ })