Patch update user bug

This commit is contained in:
Filip Sodić
2025-02-13 16:10:02 +01:00
parent 496480509a
commit e1a4ee8ab0
4 changed files with 26 additions and 26 deletions

View File

@ -357,13 +357,13 @@ action stripePayment {
entities: [User] entities: [User]
} }
action updateCurrentUser { action updateCurrentUserLastActiveTimestamp {
fn: import { updateCurrentUser } from "@src/server/actions.js", fn: import { updateCurrentUserLastActiveTimestamp } from "@src/server/actions.js",
entities: [User] entities: [User]
} }
action updateUserById { action updateIsUserAdminById {
fn: import { updateUserById } from "@src/server/actions.js", fn: import { updateIsUserAdminById } from "@src/server/actions.js",
entities: [User] entities: [User]
} }

View File

@ -1,5 +1,5 @@
import { useAuth } from 'wasp/client/auth'; import { useAuth } from 'wasp/client/auth';
import { updateCurrentUser } from 'wasp/client/operations'; import { updateCurrentUserLastActiveTimestamp } from 'wasp/client/operations';
import './Main.css'; import './Main.css';
import AppNavBar from './components/AppNavBar'; import AppNavBar from './components/AppNavBar';
import { useMemo, useEffect, ReactNode } from 'react'; import { useMemo, useEffect, ReactNode } from 'react';
@ -26,7 +26,7 @@ export default function App({ children }: { children: ReactNode }) {
const lastSeenAt = new Date(user.lastActiveTimestamp); const lastSeenAt = new Date(user.lastActiveTimestamp);
const today = new Date(); const today = new Date();
if (today.getTime() - lastSeenAt.getTime() > 5 * 60 * 1000) { if (today.getTime() - lastSeenAt.getTime() > 5 * 60 * 1000) {
updateCurrentUser({ lastActiveTimestamp: today }); updateCurrentUserLastActiveTimestamp({ lastActiveTimestamp: today });
} }
} }
}, [user]); }, [user]);

View File

@ -1,4 +1,4 @@
import { updateUserById, useQuery, getPaginatedUsers } from 'wasp/client/operations'; import { updateIsUserAdminById, useQuery, getPaginatedUsers } from 'wasp/client/operations';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import SwitcherOne from './SwitcherOne'; import SwitcherOne from './SwitcherOne';
import Loader from '../common/Loader'; import Loader from '../common/Loader';
@ -218,7 +218,7 @@ const UsersTable = () => {
</div> </div>
<div className='col-span-1 flex items-center'> <div className='col-span-1 flex items-center'>
<div className='text-sm text-black dark:text-white'> <div className='text-sm text-black dark:text-white'>
<SwitcherOne user={user} updateUserById={updateUserById} /> <SwitcherOne user={user} updateUserById={updateIsUserAdminById} />
</div> </div>
</div> </div>
<div className='col-span-1 flex items-center'> <div className='col-span-1 flex items-center'>

View File

@ -3,8 +3,8 @@ import { HttpError } from 'wasp/server';
import { import {
type GenerateGptResponse, type GenerateGptResponse,
type StripePayment, type StripePayment,
type UpdateCurrentUser, type UpdateCurrentUserLastActiveTimestamp,
type UpdateUserById, type UpdateIsUserAdminById,
type CreateTask, type CreateTask,
type DeleteTask, type DeleteTask,
type UpdateTask, type UpdateTask,
@ -286,23 +286,23 @@ export const deleteTask: DeleteTask<Pick<Task, 'id'>, Task> = async ({ id }, con
return task; return task;
}; };
export const updateUserById: UpdateUserById<{ id: number; data: Partial<User> }, User> = async ( export const updateIsUserAdminById: UpdateIsUserAdminById<{ id: number; data: Pick<User, 'isAdmin'> }, User> = async (
{ id, data }, { id, data },
context context
) => { ) => {
if (!context.user) { if (!context.user) {
throw new HttpError(401); throw new HttpError(401);
} }
if (!context.user.isAdmin) { if (!context.user.isAdmin) {
throw new HttpError(403); throw new HttpError(403);
} }
const updatedUser = await context.entities.User.update({ const updatedUser = await context.entities.User.update({
where: { where: {
id, id,
}, },
data, data: {
isAdmin: data.isAdmin,
},
}); });
return updatedUser; return updatedUser;
@ -333,15 +333,15 @@ export const createFile: CreateFile<fileArgs, File> = async ({ fileType, name },
}); });
}; };
export const updateCurrentUser: UpdateCurrentUser<Partial<User>, User> = async (user, context) => { export const updateCurrentUserLastActiveTimestamp: UpdateCurrentUserLastActiveTimestamp<Pick<User, 'lastActiveTimestamp'>, User> =
async ({ lastActiveTimestamp }, context) => {
if (!context.user) { if (!context.user) {
throw new HttpError(401); throw new HttpError(401);
} }
return context.entities.User.update({ return context.entities.User.update({
where: { where: {
id: context.user.id, id: context.user.id,
}, },
data: user, data: { lastActiveTimestamp },
}); });
}; };