Skip to content

Commit

Permalink
Merge pull request #1772 from serlo/enmeshed-adjusts
Browse files Browse the repository at this point in the history
enmeshed adjusts
  • Loading branch information
hugotiburtino authored Oct 16, 2024
2 parents 9a62ca0 + 6c102f5 commit 7deae66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/error-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function assertAll(
export function captureErrorEvent(event: ErrorEvent) {
if (process.env.ENVIRONMENT === 'local') {
// eslint-disable-next-line no-console
console.error(event.error)
console.error(event)
}

Sentry.captureException(event.error, (scope) => {
Expand Down
67 changes: 23 additions & 44 deletions packages/server/src/internals/server/enmeshed-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,6 @@ enum RelationshipAttributeConfidentiality {
Public = 'public',
}

const RelationshipAuditLogReason = t.union([
t.literal(ConnectorRelationshipAuditLogEntryReason.Creation),
t.literal(ConnectorRelationshipAuditLogEntryReason.Termination),
])

type RelationshipAuditLogReason = t.TypeOf<typeof RelationshipAuditLogReason>

const RelationshipAuditLogStatus = t.union([
t.literal(ConnectorRelationshipStatus.Pending),
t.literal(ConnectorRelationshipStatus.Rejected),
])

const RelationshipCreationContent = t.type({
'@type': t.literal('RelationshipCreationContent'),
response: t.type({
'@type': t.literal('Response'),
}),
})

const ArbitraryRelationshipCreationContent = t.type({
'@type': t.literal('ArbitraryRelationshipCreationContent'),
value: t.unknown,
})

const Relationship = t.type({
id: t.string,
peer: t.string,
Expand All @@ -101,14 +77,11 @@ const Relationship = t.type({
}),
auditLog: t.array(
t.type({
reason: RelationshipAuditLogReason,
newStatus: RelationshipAuditLogStatus,
reason: t.string,
newStatus: t.string,
}),
),
creationContent: t.union([
RelationshipCreationContent,
ArbitraryRelationshipCreationContent,
]),
creationContent: t.unknown,
})

type Relationship = t.TypeOf<typeof Relationship>
Expand Down Expand Up @@ -469,9 +442,13 @@ function createEnmeshedWebhookMiddleware(
if (!EventBody.is(body)) {
captureErrorEvent({
error: new Error('Illegal body event'),
errorContext: { body, route: '/enmeshed/webhook' },
errorContext: {
body,
validationError: EventBody.decode(body),
route: '/enmeshed/webhook',
},
})
res.status(400).send('Illegal trigger body')
res.status(400).send('Illegal body event')
return
}

Expand All @@ -483,22 +460,24 @@ function createEnmeshedWebhookMiddleware(

for (const auditLogEntry of data.auditLog) {
if (
[ConnectorRelationshipAuditLogEntryReason.Creation].includes(
auditLogEntry.reason,
) &&
(ConnectorRelationshipAuditLogEntryReason.Creation as string) ===
auditLogEntry.reason &&
[
ConnectorRelationshipStatus.Pending,
ConnectorRelationshipStatus.Rejected,
ConnectorRelationshipStatus.Pending as string,
ConnectorRelationshipStatus.Rejected as string,
].includes(auditLogEntry.newStatus)
) {
const acceptRelationshipResponse =
await client.relationships.acceptRelationship(data.id)
if (acceptRelationshipResponse.isError) {
handleConnectorError({
error: acceptRelationshipResponse.error,
message: 'Failed while accepting relationship request',
})
if (data.status !== (ConnectorRelationshipStatus.Active as string)) {
const acceptRelationshipResponse =
await client.relationships.acceptRelationship(data.id)
if (acceptRelationshipResponse.isError) {
handleConnectorError({
error: acceptRelationshipResponse.error,
message: 'Failed while accepting relationship request',
})
}
}

if (!sessionId) {
await sendWelcomeMessage({ relationship: data, client })
await sendAttributesChangeRequest({ relationship: data, client })
Expand Down

0 comments on commit 7deae66

Please sign in to comment.