Skip to content

Commit 2b1ec80

Browse files
committed
improve invitation fetching
1 parent eefd081 commit 2b1ec80

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

apps/server/src/handlers/listInvitations.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import type { SpaceState } from 'graph-framework-space-events';
1+
import { Schema } from 'effect';
2+
import { SpaceState } from 'graph-framework-space-events';
23
import { prisma } from '../prisma.js';
34

45
type Params = {
56
accountId: string;
67
};
78

9+
const decodeSpaceState = Schema.decodeUnknownEither(SpaceState);
10+
811
export const listInvitations = async ({ accountId }: Params) => {
912
const result = await prisma.invitation.findMany({
1013
where: {
@@ -24,12 +27,19 @@ export const listInvitations = async ({ accountId }: Params) => {
2427
},
2528
});
2629

27-
return result.map((invitation) => {
28-
const state = JSON.parse(invitation.space.events[0].state) as SpaceState;
29-
return {
30-
id: invitation.id,
31-
previousEventHash: state.lastEventHash,
32-
spaceId: invitation.spaceId,
33-
};
34-
});
30+
return result
31+
.map((invitation) => {
32+
const result = decodeSpaceState(JSON.parse(invitation.space.events[0].state));
33+
if (result._tag === 'Right') {
34+
const state = result.right;
35+
return {
36+
id: invitation.id,
37+
previousEventHash: state.lastEventHash,
38+
spaceId: invitation.spaceId,
39+
};
40+
}
41+
console.error('Invalid space state from the DB', result.left);
42+
return null;
43+
})
44+
.filter((invitation) => invitation !== null);
3545
};

0 commit comments

Comments
 (0)