Skip to content

Commit 139a385

Browse files
authored
Fix/redundant-get-fsrs (#20)
1 parent 0bef1b6 commit 139a385

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/lib/card.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ function getUpdateCardPayloadByScheduler(recordItem:RepeatRecordLog,duration:num
9898
}
9999

100100
export async function updateCard(cid:number,now:Date,grade:Grade,duration:number){
101-
const [_,recordItem]=await Promise.all([getFSRS(cid,true), schedulerCard({cid},now,Number(grade) as Grade)])
101+
const recordItem = await schedulerCard(
102+
{ cid },
103+
now,
104+
Number(grade) as Grade
105+
);
102106
const payload = getUpdateCardPayloadByScheduler(recordItem,duration)
103107
await prisma.card.update({
104108
where:{cid:cid},

src/lib/fsrs.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,22 @@ export async function updateParameters(params: FSRSPutParams) {
111111
})
112112
}
113113

114-
export function getFSRS(cid: number): Promise<{f:FSRS,userParams:ParametersType}>;
115-
export function getFSRS<T extends boolean>(cid: number, skip: T): Promise<T extends true ? null : {f:FSRS,userParams:ParametersType}>;
116-
export async function getFSRS<T extends boolean = boolean>
117-
(cid:number,skip: T=false as T)
118-
: Promise<T extends true ? null : {f:FSRS,userParams:ParametersType}> {
119-
const userParams = await getFSRSParamsByCid(cid)
120-
const permission = await isAdminOrSelf(userParams.uid)
121-
if(!permission){
122-
throw new Error("permission denied")
123-
}
124-
if(skip){
125-
return null as T extends true ? null : {f:FSRS,userParams:ParametersType};
126-
}
127-
const f = fsrs(userParams.params) as FSRS
128-
return {
129-
f,
130-
userParams
131-
} as T extends true ? null : {f:FSRS,userParams:ParametersType};
114+
115+
/**
116+
* verify if the user has the permission to access the FSRS
117+
* if the user is the admin or the owner of the FSRS, return the FSRS and the user parameters
118+
* @param cid card id
119+
* @throws permission denied
120+
*/
121+
export async function getFSRS(cid: number) {
122+
const userParams = await getFSRSParamsByCid(cid);
123+
const permission = await isAdminOrSelf(userParams.uid);
124+
if (!permission) {
125+
throw new Error("permission denied");
126+
}
127+
const f = fsrs(userParams.params) as FSRS;
128+
return {
129+
f,
130+
userParams,
131+
} as { f: FSRS; userParams: ParametersType };
132132
}

0 commit comments

Comments
 (0)