Skip to content

Commit

Permalink
Merge branch 'dev' into api-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 authored Dec 27, 2024
2 parents abecb7d + 640026c commit db96ef8
Show file tree
Hide file tree
Showing 49 changed files with 443 additions and 255 deletions.
9 changes: 9 additions & 0 deletions apps/backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @stackframe/stack-backend

## 2.7.1

### Patch Changes

- Various changes
- Updated dependencies
- @stackframe/stack-emails@2.7.1
- @stackframe/stack-shared@2.7.1

## 2.7.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackframe/stack-backend",
"version": "2.7.0",
"version": "2.7.1",
"private": true,
"scripts": {
"clean": "rimraf .next && rimraf node_modules",
Expand Down
1 change: 0 additions & 1 deletion apps/backend/src/app/api/v1/internal/metrics/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async function loadUsersByCountry(projectId: string): Promise<Record<string, num
GROUP BY "countryCode"
ORDER BY "userCount" DESC;
`;
console.log("AAAAAAAAAA", a);

const rec = Object.fromEntries(
a.map(({ userCount, countryCode }) => [countryCode, Number(userCount)])
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/api/v1/team-member-profiles/crud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const teamMemberProfilesCrudHandlers = createLazyProxy(() => createCrudHa
throw new KnownErrors.TeamMembershipNotFound(params.team_id, params.user_id);
}

return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId, db.projectUser.createdAt));
return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
});
},
onUpdate: async ({ auth, data, params }) => {
Expand Down Expand Up @@ -151,7 +151,7 @@ export const teamMemberProfilesCrudHandlers = createLazyProxy(() => createCrudHa
include: fullInclude,
});

return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId, db.projectUser.createdAt));
return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
});
},
}));
36 changes: 16 additions & 20 deletions apps/backend/src/app/api/v1/teams/crud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,27 @@ export const teamsCrudHandlers = createLazyProxy(() => createCrudHandlers(teamsC
return result;
},
onRead: async ({ params, auth }) => {
const db = await retryTransaction(async (tx) => {
if (auth.type === 'client') {
await ensureTeamMembershipExists(tx, {
if (auth.type === 'client') {
await ensureTeamMembershipExists(prismaClient, {
projectId: auth.project.id,
teamId: params.team_id,
userId: auth.user?.id ?? throwErr(new KnownErrors.UserAuthenticationRequired),
});
}

const db = await prismaClient.team.findUnique({
where: {
projectId_teamId: {
projectId: auth.project.id,
teamId: params.team_id,
userId: auth.user?.id ?? throwErr(new KnownErrors.UserAuthenticationRequired),
});
}

const db = await prismaClient.team.findUnique({
where: {
projectId_teamId: {
projectId: auth.project.id,
teamId: params.team_id,
},
},
});

if (!db) {
throw new KnownErrors.TeamNotFound(params.team_id);
}

return db;
},
});

if (!db) {
throw new KnownErrors.TeamNotFound(params.team_id);
}

return teamPrismaToCrud(db);
},
onUpdate: async ({ params, auth, data }) => {
Expand Down
14 changes: 6 additions & 8 deletions apps/backend/src/app/api/v1/users/crud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async function getOtpConfig(tx: PrismaTransaction, projectConfigId: string) {
return otpConfig.length === 0 ? null : otpConfig[0];
}

export const getUserLastActiveAtMillis = async (userId: string, fallbackTo: number | Date): Promise<number> => {
export const getUserLastActiveAtMillis = async (userId: string): Promise<number | null> => {
const event = await prismaClient.event.findFirst({
where: {
data: {
Expand All @@ -216,9 +216,7 @@ export const getUserLastActiveAtMillis = async (userId: string, fallbackTo: numb
},
});

return event?.createdAt.getTime() ?? (
typeof fallbackTo === "number" ? fallbackTo : fallbackTo.getTime()
);
return event?.createdAt.getTime() ?? null;
};

// same as userIds.map(userId => getUserLastActiveAtMillis(userId, fallbackTo)), but uses a single query
Expand Down Expand Up @@ -254,14 +252,14 @@ export async function getUser(options: { projectId: string, userId: string }) {
},
include: userFullInclude,
}),
getUserLastActiveAtMillis(options.userId, new Date()),
getUserLastActiveAtMillis(options.userId),
]);

if (!db) {
return null;
}

return userPrismaToCrud(db, lastActiveAtMillis);
return userPrismaToCrud(db, lastActiveAtMillis ?? db.createdAt.getTime());
}

export const usersCrudHandlers = createLazyProxy(() => createCrudHandlers(usersCrud, {
Expand Down Expand Up @@ -516,7 +514,7 @@ export const usersCrudHandlers = createLazyProxy(() => createCrudHandlers(usersC
throw new StackAssertionError("User was created but not found", newUser);
}

return userPrismaToCrud(user, await getUserLastActiveAtMillis(user.projectUserId, new Date()));
return userPrismaToCrud(user, await getUserLastActiveAtMillis(user.projectUserId) ?? user.createdAt.getTime());
});

if (auth.project.config.create_team_on_sign_up) {
Expand Down Expand Up @@ -828,7 +826,7 @@ export const usersCrudHandlers = createLazyProxy(() => createCrudHandlers(usersC
});
}

return userPrismaToCrud(db, await getUserLastActiveAtMillis(params.user_id, new Date()));
return userPrismaToCrud(db, await getUserLastActiveAtMillis(params.user_id) ?? db.createdAt.getTime());
});


Expand Down
Loading

0 comments on commit db96ef8

Please sign in to comment.