Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(enmeshed): Fix endpoints to reenable user journey #1144

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All notable changes to this project will be documented in this file.

## [v0.59.2](https://github.com/serlo/api.serlo.org/compare/v0.59.1..v0.59.2) - December 1, 2023

### Fixed

- Fix enmeshed endpoints to reenable user journey

- Set always a ttl for each key

### Internal

- Add pact tests to github repo

- Remove unnecessary test redis

- Improve CI speed

- Make tests run parallel

## [v0.59.1](https://github.com/serlo/api.serlo.org/compare/v0.59.0..v0.59.1) - November 30, 2023

### Internal
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"packages/*"
],
"npmClient": "yarn",
"version": "0.59.1",
"version": "0.59.2",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion packages/authorization/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serlo/authorization",
"version": "0.59.1",
"version": "0.59.2",
"repository": "serlo/api.serlo.org",
"license": "Apache-2.0",
"author": "Serlo Education e.V.",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serlo/api.serlo.org",
"version": "0.59.1",
"version": "0.59.2",
"private": true,
"repository": "serlo/api.serlo.org",
"license": "Apache-2.0",
Expand Down Expand Up @@ -32,7 +32,7 @@
"@nmshd/connector-sdk": "^2.2.4",
"@ory/client": "^1.4.3",
"@sentry/node": "^7.82.0",
"@serlo/authorization": "^0.59.1",
"@serlo/authorization": "^0.59.2",
"@types/basic-auth": "^1.1.6",
"@types/bull-arena": "^3.0.10",
"@types/jsonwebtoken": "^9.0.5",
Expand Down
95 changes: 58 additions & 37 deletions packages/server/src/internals/server/enmeshed-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function applyEnmeshedMiddleware({
return `${basePath}/init`
}

const EventBody = t.type({
const GenericEventBody = t.type({
trigger: t.string,
})

Expand All @@ -80,9 +80,24 @@ const Relationship = t.type({

type Relationship = t.TypeOf<typeof Relationship>

const RelationshipChangedEventBody = t.type({
data: Relationship,
trigger: t.literal('transport.relationshipChanged'),
const Attribute = t.type({
id: t.string,
content: t.type({
'@type': t.union([
t.literal('IdentityAttribute'),
t.literal('RelationshipAttribute'),
]),
owner: t.string,
value: t.unknown,
}),
})

const EventBody = t.type({
data: t.union([Relationship, Attribute]),
trigger: t.union([
t.literal('transport.relationshipChanged'),
t.literal('consumption.attributeCreated'),
]),
})

const Session = t.intersection([
Expand Down Expand Up @@ -397,62 +412,68 @@ function createEnmeshedWebhookMiddleware(

const body = req.body as unknown

if (!EventBody.is(body)) {
if (!GenericEventBody.is(body)) {
res.status(400).send('Illegal trigger body')
return
}

if (body.trigger !== 'transport.relationshipChanged') {
if (
body.trigger !== 'transport.relationshipChanged' &&
body.trigger !== 'consumption.attributeCreated'
) {
res.sendStatus(200)
return
}

if (!RelationshipChangedEventBody.is(body)) {
if (!EventBody.is(body)) {
captureErrorEvent({
error: new Error('Illegal body for relationship change event'),
error: new Error('Illegal body event'),
errorContext: { body, route: '/enmeshed/webhook' },
})
res.status(400).send('Illegal trigger body')
return
}

const relationship = body.data

const sessionId =
relationship.template.content?.onNewRelationship?.metadata?.sessionId ??
null

for (const change of relationship.changes) {
if (
[ConnectorRelationshipChangeType.CREATION as string].includes(
change.type,
) &&
[
ConnectorRelationshipChangeStatus.PENDING as string,
ConnectorRelationshipChangeStatus.REJECTED as string,
].includes(change.status)
) {
await acceptRelationshipRequest(relationship, change, client)
if (!sessionId) {
await sendWelcomeMessage({ relationship, client })
await sendAttributesChangeRequest({ relationship, client })
const data = body.data

if (Relationship.is(data)) {
const sessionId =
data.template.content?.onNewRelationship?.metadata?.sessionId ?? null

for (const change of data.changes) {
if (
[ConnectorRelationshipChangeType.CREATION as string].includes(
change.type,
) &&
[
ConnectorRelationshipChangeStatus.PENDING as string,
ConnectorRelationshipChangeStatus.REJECTED as string,
].includes(change.status)
) {
await acceptRelationshipRequest(data, change, client)
if (!sessionId) {
await sendWelcomeMessage({ relationship: data, client })
await sendAttributesChangeRequest({ relationship: data, client })
}
}
}
}

// FIXME: Uncomment next line when prototype frontend has been replaced
// if (!sessionId) return validationError(res, 'Missing required parameter: sessionId.')
const session = await getSession(cache, sessionId)
// FIXME: Uncomment next line when prototype frontend has been replaced
// if (!sessionId) return validationError(res, 'Missing required parameter: sessionId.')
const session = await getSession(cache, sessionId)

if (session) {
await setSession(cache, sessionId, {
relationshipTemplateId: relationship.template.id,
content: relationship.template.content as Session['content'],
})
if (session) {
await setSession(cache, sessionId, {
relationshipTemplateId: data.template.id,
enmeshedId: data.peer,
content: data.template.content as Session['content'],
})
}
}

res.status(200).end('')
}

return (request, response) => {
handleRequest(request, response).catch((error: Error) => {
captureErrorEvent({
Expand Down
14 changes: 14 additions & 0 deletions scripts/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,20 @@ async function exec(): Promise<void> {
date: '2023-11-30',
internal: ['`@serlo/authorization`: Remove dependency of `@ſerlo/api`'],
},
{
tagName: 'v0.59.2',
date: '2023-12-01',
internal: [
'Add pact tests to github repo',
'Remove unnecessary test redis',
'Improve CI speed',
'Make tests run parallel',
],
fixed: [
'Fix enmeshed endpoints to reenable user journey',
'Set always a ttl for each key',
],
},
],
})

Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4096,7 +4096,7 @@ __metadata:
"@nmshd/connector-sdk": ^2.2.4
"@ory/client": ^1.4.3
"@sentry/node": ^7.82.0
"@serlo/authorization": ^0.59.1
"@serlo/authorization": ^0.59.2
"@types/basic-auth": ^1.1.6
"@types/bull-arena": ^3.0.10
"@types/jsonwebtoken": ^9.0.5
Expand Down Expand Up @@ -4137,7 +4137,7 @@ __metadata:
languageName: unknown
linkType: soft

"@serlo/authorization@^0.59.1, @serlo/authorization@workspace:packages/authorization":
"@serlo/authorization@^0.59.2, @serlo/authorization@workspace:packages/authorization":
version: 0.0.0-use.local
resolution: "@serlo/authorization@workspace:packages/authorization"
languageName: unknown
Expand Down