@ossy/workspaces 3.1.0 → 3.4.0
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.
- package/package.json +3 -3
- package/src/invite-user.task.js +0 -10
- package/src/server.js +1 -0
- package/src/workspace.aggregate.spec.js +280 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/workspaces",
|
|
3
3
|
"description": "Workspaces feature package - create, select, and manage workspaces, users, and invitations",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"module": "./src/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@ossy/config": "^3.0.9",
|
|
27
|
-
"@ossy/schema": "^3.0
|
|
27
|
+
"@ossy/schema": "^3.4.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@ossy/design-system": ">=1.0.0",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"/src",
|
|
46
46
|
"README.md"
|
|
47
47
|
],
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "d36b69444268d172bc3e8e1dc77e67afc63f8650"
|
|
49
49
|
}
|
package/src/invite-user.task.js
CHANGED
|
@@ -5,13 +5,10 @@ import { Workspace, WorkspacesEvents } from '@ossy/workspaces/server'
|
|
|
5
5
|
import { Token, TokenEvents } from '@ossy/tokens/server'
|
|
6
6
|
import { EmailRenderer } from '@ossy/email'
|
|
7
7
|
import { ConfigService } from '@ossy/config'
|
|
8
|
-
import { createLogger } from '@ossy/observability'
|
|
9
8
|
import WorkspaceInvitationEmail, { subject as workspaceInvitationSubject } from './workspace-invitation.email.jsx'
|
|
10
9
|
|
|
11
10
|
export const metadata = { id: '@ossy/workspaces/tasks/invite-user' }
|
|
12
11
|
|
|
13
|
-
const log = createLogger('@ossy/workspaces/tasks/invite-user')
|
|
14
|
-
|
|
15
12
|
const isEmailLike = maybeEmail => /.@.+\.../.test(maybeEmail)
|
|
16
13
|
|
|
17
14
|
function createVerificationToken(workspaceId) {
|
|
@@ -72,13 +69,6 @@ export async function run({ payload, integrations, req }) {
|
|
|
72
69
|
const emailClient = integrations?.get?.('email')
|
|
73
70
|
if (emailClient) {
|
|
74
71
|
await emailClient.send({ to: email, from: 'noreply@ossy.se', subject: workspaceInvitationSubject, html, text })
|
|
75
|
-
} else if (ConfigService.BuildEnvironment === 'local') {
|
|
76
|
-
log.info('-----------YOU GOT MAIL-----------')
|
|
77
|
-
log.info(`To: ${email}`)
|
|
78
|
-
log.info(`From: noreply@ossy.se`)
|
|
79
|
-
log.info(`Subject: ${workspaceInvitationSubject}`)
|
|
80
|
-
log.info(text || html)
|
|
81
|
-
log.info('----------------------------------')
|
|
82
72
|
}
|
|
83
73
|
|
|
84
74
|
return ''
|
package/src/server.js
CHANGED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals'
|
|
2
|
+
import { Workspace } from './workspace.aggregate.js'
|
|
3
|
+
import { WorkspacesEvents } from './workspaces.events.js'
|
|
4
|
+
import { WorkspaceSchema } from './schema-ids.js'
|
|
5
|
+
|
|
6
|
+
const NOW = 1_700_000_000_000
|
|
7
|
+
const DAY = 1000 * 60 * 60 * 24
|
|
8
|
+
const WEEK = DAY * 7
|
|
9
|
+
|
|
10
|
+
function createdEvent (overrides = {}) {
|
|
11
|
+
return {
|
|
12
|
+
...WorkspacesEvents.Created({
|
|
13
|
+
resourceId: 'ws-1',
|
|
14
|
+
createdBy: 'user-1',
|
|
15
|
+
name: 'Acme',
|
|
16
|
+
}),
|
|
17
|
+
created: NOW,
|
|
18
|
+
...overrides,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function withMeta (event, overrides = {}) {
|
|
23
|
+
return {
|
|
24
|
+
...event,
|
|
25
|
+
created: NOW + 1000,
|
|
26
|
+
...overrides,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('Workspace.View', () => {
|
|
31
|
+
|
|
32
|
+
describe('empty event list', () => {
|
|
33
|
+
it('returns savedState with normalized empty services when no events are given', () => {
|
|
34
|
+
expect(Workspace.View([], { id: 'ws-1', extra: 'data' })).toEqual({
|
|
35
|
+
id: 'ws-1',
|
|
36
|
+
extra: 'data',
|
|
37
|
+
services: {},
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('uses {} as the default savedState', () => {
|
|
42
|
+
expect(Workspace.View([])).toEqual({ services: {} })
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('Created event', () => {
|
|
47
|
+
it('sets id from resourceId', () => {
|
|
48
|
+
expect(Workspace.View([createdEvent()]).id).toBe('ws-1')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('sets type from the workspace schema', () => {
|
|
52
|
+
expect(Workspace.View([createdEvent()]).type).toBe(WorkspaceSchema.workspace)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('sets name from payload', () => {
|
|
56
|
+
expect(Workspace.View([createdEvent()]).name).toBe('Acme')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('sets createdBy and created from the event', () => {
|
|
60
|
+
const result = Workspace.View([createdEvent()])
|
|
61
|
+
expect(result.createdBy).toBe('user-1')
|
|
62
|
+
expect(result.created).toBe(NOW)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('initializes users with the creator and empty collections', () => {
|
|
66
|
+
const result = Workspace.View([createdEvent()])
|
|
67
|
+
expect(result.users).toEqual(['user-1'])
|
|
68
|
+
expect(result.invitations).toEqual([])
|
|
69
|
+
expect(result.apiTokens).toEqual([])
|
|
70
|
+
expect(result.schemas).toEqual([])
|
|
71
|
+
expect(result.services).toEqual({})
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('SchemasImported', () => {
|
|
76
|
+
it('replaces schemas from the payload', () => {
|
|
77
|
+
const schemas = [
|
|
78
|
+
{ id: '@ossy/booking/schema/booking', label: 'Booking' },
|
|
79
|
+
]
|
|
80
|
+
const result = Workspace.View([
|
|
81
|
+
createdEvent(),
|
|
82
|
+
withMeta(WorkspacesEvents.SchemasImported({
|
|
83
|
+
createdBy: 'user-1',
|
|
84
|
+
schemas,
|
|
85
|
+
})),
|
|
86
|
+
])
|
|
87
|
+
expect(result.schemas).toEqual(schemas)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('treats missing schemas as an empty list', () => {
|
|
91
|
+
const result = Workspace.View([
|
|
92
|
+
createdEvent(),
|
|
93
|
+
withMeta({
|
|
94
|
+
event: 'SchemasImported',
|
|
95
|
+
createdBy: 'user-1',
|
|
96
|
+
payload: {},
|
|
97
|
+
}),
|
|
98
|
+
])
|
|
99
|
+
expect(result.schemas).toEqual([])
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
describe('UserInvited', () => {
|
|
104
|
+
it('appends a pending invitation that has not expired', () => {
|
|
105
|
+
const expiresAt = Date.now() + DAY
|
|
106
|
+
const result = Workspace.View([
|
|
107
|
+
createdEvent(),
|
|
108
|
+
withMeta(WorkspacesEvents.UserInvited({
|
|
109
|
+
createdBy: 'user-1',
|
|
110
|
+
email: 'new@example.com',
|
|
111
|
+
expiresAt,
|
|
112
|
+
}), { created: NOW + 2000 }),
|
|
113
|
+
])
|
|
114
|
+
expect(result.invitations).toEqual([{
|
|
115
|
+
email: 'new@example.com',
|
|
116
|
+
invitedAt: NOW + 2000,
|
|
117
|
+
expiresAt,
|
|
118
|
+
expired: false,
|
|
119
|
+
}])
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('keeps recently expired invitations marked as expired', () => {
|
|
123
|
+
const expiresAt = Date.now() - DAY
|
|
124
|
+
const result = Workspace.View([
|
|
125
|
+
createdEvent(),
|
|
126
|
+
withMeta(WorkspacesEvents.UserInvited({
|
|
127
|
+
createdBy: 'user-1',
|
|
128
|
+
email: 'late@example.com',
|
|
129
|
+
expiresAt,
|
|
130
|
+
}), { created: NOW + 2000 }),
|
|
131
|
+
])
|
|
132
|
+
expect(result.invitations).toHaveLength(1)
|
|
133
|
+
expect(result.invitations[0]).toMatchObject({
|
|
134
|
+
email: 'late@example.com',
|
|
135
|
+
expired: true,
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('hides invitations expired more than one week ago', () => {
|
|
140
|
+
const expiresAt = Date.now() - WEEK - DAY
|
|
141
|
+
const result = Workspace.View([
|
|
142
|
+
createdEvent(),
|
|
143
|
+
withMeta(WorkspacesEvents.UserInvited({
|
|
144
|
+
createdBy: 'user-1',
|
|
145
|
+
email: 'gone@example.com',
|
|
146
|
+
expiresAt,
|
|
147
|
+
})),
|
|
148
|
+
])
|
|
149
|
+
expect(result.invitations).toEqual([])
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('UserInvitationAccepted', () => {
|
|
154
|
+
it('adds the accepting user and removes their invitation', () => {
|
|
155
|
+
const expiresAt = Date.now() + DAY
|
|
156
|
+
const result = Workspace.View([
|
|
157
|
+
createdEvent(),
|
|
158
|
+
withMeta(WorkspacesEvents.UserInvited({
|
|
159
|
+
createdBy: 'user-1',
|
|
160
|
+
email: 'new@example.com',
|
|
161
|
+
expiresAt,
|
|
162
|
+
})),
|
|
163
|
+
withMeta(WorkspacesEvents.UserInvitationAccepted({
|
|
164
|
+
createdBy: 'user-2',
|
|
165
|
+
email: 'new@example.com',
|
|
166
|
+
}), { created: NOW + 3000 }),
|
|
167
|
+
])
|
|
168
|
+
expect(result.users).toEqual(['user-1', 'user-2'])
|
|
169
|
+
expect(result.invitations).toEqual([])
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe('ApiTokenCreated', () => {
|
|
174
|
+
it('appends a token using the event id and description', () => {
|
|
175
|
+
const result = Workspace.View([
|
|
176
|
+
createdEvent(),
|
|
177
|
+
withMeta(WorkspacesEvents.ApiTokenCreated({
|
|
178
|
+
createdBy: 'user-1',
|
|
179
|
+
description: 'CI token',
|
|
180
|
+
token: 'secret',
|
|
181
|
+
}), { id: 'token-1' }),
|
|
182
|
+
])
|
|
183
|
+
expect(result.apiTokens).toEqual([
|
|
184
|
+
{ id: 'token-1', description: 'CI token' },
|
|
185
|
+
])
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
describe('ServiceEnabled and ServiceDisabled', () => {
|
|
190
|
+
it('enables a service under its npm package key', () => {
|
|
191
|
+
const result = Workspace.View([
|
|
192
|
+
createdEvent(),
|
|
193
|
+
withMeta(WorkspacesEvents.ServiceEnabled({
|
|
194
|
+
createdBy: 'user-1',
|
|
195
|
+
service: '@ossy/booking',
|
|
196
|
+
})),
|
|
197
|
+
])
|
|
198
|
+
expect(result.services).toEqual({
|
|
199
|
+
'@ossy/booking': { enabled: true },
|
|
200
|
+
})
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('disables a previously enabled service', () => {
|
|
204
|
+
const result = Workspace.View([
|
|
205
|
+
createdEvent(),
|
|
206
|
+
withMeta(WorkspacesEvents.ServiceEnabled({
|
|
207
|
+
createdBy: 'user-1',
|
|
208
|
+
service: '@ossy/booking',
|
|
209
|
+
})),
|
|
210
|
+
withMeta(WorkspacesEvents.ServiceDisabled({
|
|
211
|
+
createdBy: 'user-1',
|
|
212
|
+
service: '@ossy/booking',
|
|
213
|
+
}), { created: NOW + 4000 }),
|
|
214
|
+
])
|
|
215
|
+
expect(result.services).toEqual({
|
|
216
|
+
'@ossy/booking': { enabled: false },
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it('drops non-scoped service keys via normalizeWorkspaceServices', () => {
|
|
221
|
+
const result = Workspace.View([
|
|
222
|
+
createdEvent(),
|
|
223
|
+
withMeta(WorkspacesEvents.ServiceEnabled({
|
|
224
|
+
createdBy: 'user-1',
|
|
225
|
+
service: 'booking',
|
|
226
|
+
})),
|
|
227
|
+
])
|
|
228
|
+
expect(result.services).toEqual({})
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
describe('UserRemoved', () => {
|
|
233
|
+
it('removes the user from the workspace members list', () => {
|
|
234
|
+
const result = Workspace.View([
|
|
235
|
+
createdEvent(),
|
|
236
|
+
withMeta(WorkspacesEvents.UserInvitationAccepted({
|
|
237
|
+
createdBy: 'user-2',
|
|
238
|
+
email: 'new@example.com',
|
|
239
|
+
})),
|
|
240
|
+
withMeta(WorkspacesEvents.UserRemoved({
|
|
241
|
+
createdBy: 'user-1',
|
|
242
|
+
userId: 'user-2',
|
|
243
|
+
}), { created: NOW + 5000 }),
|
|
244
|
+
])
|
|
245
|
+
expect(result.users).toEqual(['user-1'])
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
describe('unknown event type', () => {
|
|
250
|
+
it('ignores unknown events and returns state unchanged aside from services normalization', () => {
|
|
251
|
+
const unknown = { event: 'SomethingElse', payload: {} }
|
|
252
|
+
const result = Workspace.View([createdEvent(), unknown])
|
|
253
|
+
expect(result.name).toBe('Acme')
|
|
254
|
+
expect(result.users).toEqual(['user-1'])
|
|
255
|
+
})
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
describe('savedState merging', () => {
|
|
259
|
+
it('merges events on top of a provided savedState', () => {
|
|
260
|
+
const savedState = {
|
|
261
|
+
id: 'ws-1',
|
|
262
|
+
name: 'Acme',
|
|
263
|
+
users: ['user-1'],
|
|
264
|
+
invitations: [],
|
|
265
|
+
services: { '@ossy/booking': { enabled: true } },
|
|
266
|
+
}
|
|
267
|
+
const result = Workspace.View([
|
|
268
|
+
withMeta(WorkspacesEvents.UserRemoved({
|
|
269
|
+
createdBy: 'user-1',
|
|
270
|
+
userId: 'user-1',
|
|
271
|
+
})),
|
|
272
|
+
], savedState)
|
|
273
|
+
expect(result.users).toEqual([])
|
|
274
|
+
expect(result.services).toEqual({
|
|
275
|
+
'@ossy/booking': { enabled: true },
|
|
276
|
+
})
|
|
277
|
+
expect(result.name).toBe('Acme')
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
})
|