Skip to content

Commit

Permalink
fix: Ordered workspaces by last opened (#247)
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Groß <mail@gross-johannes.de>
  • Loading branch information
jo-gross authored Apr 26, 2024
1 parent 61a8354 commit abf039a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "WorkspaceUser"
ADD COLUMN "lastOpened" TIMESTAMP(3);
13 changes: 13 additions & 0 deletions pages/api/workspaces/[workspaceId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import WorkspaceUpdateInput = Prisma.WorkspaceUpdateInput;
export default withHttpMethods({
[HTTPMethod.GET]: withWorkspacePermission([Role.USER], async (req, res, user, workspace) => {
const settings = await prisma.workspaceSetting.findMany({ where: { workspaceId: workspace.id } });

await prisma.workspaceUser.update({
where: {
workspaceId_userId: {
workspaceId: workspace.id,
userId: user.id,
},
},
data: {
lastOpened: new Date(),
},
});

return res.json({ data: { ...workspace, WorkspaceSetting: settings } });
}),
[HTTPMethod.DELETE]: withWorkspacePermission([Role.ADMIN], async (req, res, user, workspace) => {
Expand Down
11 changes: 10 additions & 1 deletion pages/api/workspaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,17 @@ export default withHttpMethods({
},
},
},
include: {
users: true,
},
});

return res.json({ data: result });
const sortedResult = result.sort(
(a, b) =>
(b.users?.find((u) => u.userId == user.id)?.lastOpened || new Date(1970, 1, 1)).getTime() -
(a.users?.find((u) => u.userId == user.id)?.lastOpened || new Date(1970, 1, 1)).getTime(),
);

return res.json({ data: sortedResult });
}),
});
2 changes: 2 additions & 0 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ model WorkspaceUser {
userId String // relation scalar field (used in the `@relation` attribute above)
role Role
lastOpened DateTime?
@@id([workspaceId, userId])
}

Expand Down

0 comments on commit abf039a

Please sign in to comment.