Skip to content

Commit

Permalink
Fix/redundant-get-fsrs (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishiko732 authored May 4, 2024
1 parent 0bef1b6 commit 139a385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/lib/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ function getUpdateCardPayloadByScheduler(recordItem:RepeatRecordLog,duration:num
}

export async function updateCard(cid:number,now:Date,grade:Grade,duration:number){
const [_,recordItem]=await Promise.all([getFSRS(cid,true), schedulerCard({cid},now,Number(grade) as Grade)])
const recordItem = await schedulerCard(
{ cid },
now,
Number(grade) as Grade
);
const payload = getUpdateCardPayloadByScheduler(recordItem,duration)
await prisma.card.update({
where:{cid:cid},
Expand Down
36 changes: 18 additions & 18 deletions src/lib/fsrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,22 @@ export async function updateParameters(params: FSRSPutParams) {
})
}

export function getFSRS(cid: number): Promise<{f:FSRS,userParams:ParametersType}>;
export function getFSRS<T extends boolean>(cid: number, skip: T): Promise<T extends true ? null : {f:FSRS,userParams:ParametersType}>;
export async function getFSRS<T extends boolean = boolean>
(cid:number,skip: T=false as T)
: Promise<T extends true ? null : {f:FSRS,userParams:ParametersType}> {
const userParams = await getFSRSParamsByCid(cid)
const permission = await isAdminOrSelf(userParams.uid)
if(!permission){
throw new Error("permission denied")
}
if(skip){
return null as T extends true ? null : {f:FSRS,userParams:ParametersType};
}
const f = fsrs(userParams.params) as FSRS
return {
f,
userParams
} as T extends true ? null : {f:FSRS,userParams:ParametersType};

/**
* verify if the user has the permission to access the FSRS
* if the user is the admin or the owner of the FSRS, return the FSRS and the user parameters
* @param cid card id
* @throws permission denied
*/
export async function getFSRS(cid: number) {
const userParams = await getFSRSParamsByCid(cid);
const permission = await isAdminOrSelf(userParams.uid);
if (!permission) {
throw new Error("permission denied");
}
const f = fsrs(userParams.params) as FSRS;
return {
f,
userParams,
} as { f: FSRS; userParams: ParametersType };
}

1 comment on commit 139a385

@vercel
Copy link

@vercel vercel bot commented on 139a385 May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.