Skip to content

Commit

Permalink
Error when no user shl exists, move user id to config for create
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrgn committed Feb 29, 2024
1 parent d772c02 commit 492c9ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
50 changes: 28 additions & 22 deletions server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ async function updateAccessToken(endpoint: types.HealthLinkEndpoint) {
}

export const DbLinks = {
create(config: types.HealthLinkConfig, user_id: string = "") {
create(config: types.HealthLinkConfig) {
let { userId, ...configSansUser } = config;

const link = {
config,
config: configSansUser,
id: randomStringWithEntropy(32),
user_id: user_id,
user_id: userId,
managementToken: randomStringWithEntropy(32),
created: new Date().toUTCString().slice(0, 19).replace('T', ' '),
created: new Date().getTime() / 1000,
active: true,
};
db.query(
Expand All @@ -57,7 +59,7 @@ export const DbLinks = {
created: link.created,
exp: link.config.exp,
passcode: link.config.passcode,
},
}
);

return link;
Expand Down Expand Up @@ -96,23 +98,27 @@ export const DbLinks = {
},
};
},
getUserShl(userId: string): types.HealthLink {
const linkRow = db
.prepareQuery(`SELECT * from shlink where user_id=? order by created desc`)
.oneEntry([userId]);

return {
id: linkRow.id as string,
passcodeFailuresRemaining: linkRow.passcode_failures_remaining as number,
active: Boolean(linkRow.active) as boolean,
user_id: linkRow.user_id as string,
created: linkRow.created as string,
managementToken: linkRow.management_token as string,
config: {
exp: linkRow.config_exp as number,
passcode: linkRow.config_passcode as string,
},
};
getUserShl(userId: string): types.HealthLink | undefined {
try {
const linkRow = db
.prepareQuery(`SELECT * from shlink where user_id=? order by created desc`)
.oneEntry([userId]);
return {
id: linkRow.id as string,
passcodeFailuresRemaining: linkRow.passcode_failures_remaining as number,
active: Boolean(linkRow.active) as boolean,
user_id: linkRow.user_id as string,
created: linkRow.created as string,
managementToken: linkRow.management_token as string,
config: {
exp: linkRow.config_exp as number,
passcode: linkRow.config_passcode as string,
},
};
} catch (e) {
console.warn(e);
return undefined;
}
},
getShlInternal(linkId: string): types.HealthLink {
const linkRow = db.prepareQuery(`SELECT * from shlink where id=?`).oneEntry([linkId]);
Expand Down
13 changes: 2 additions & 11 deletions server/routers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ export const shlApiRouter = new oak.Router()
config: undefined,
};
})
.post('/user/:userId/shl', async (context) => {
const config: types.HealthLinkConfig = await context.request.body({ type: 'json' }).value;
const newLink = db.DbLinks.create(config, context.params.userId);
context.response.body = {
...newLink,
files: undefined,
config: undefined,
};
})
.post('/shl/:shlId', async (context) => {
const config: types.HealthLinkManifestRequest = await context.request.body({ type: 'json' }).value;
const embeddedLengthMax = Math.min(env.EMBEDDED_LENGTH_MAX, config.embeddedLengthMax !== undefined ? config.embeddedLengthMax : Infinity);
Expand Down Expand Up @@ -108,9 +99,9 @@ export const shlApiRouter = new oak.Router()
.get('/user/:userId', async (context) => {
const shl = db.DbLinks.getUserShl(context.params.userId)!;
if (!shl) {
throw new Error(`Can't find SHLink for user ` + context.params.userId);
console.log(`Can't find SHLink for user ` + context.params.userId);
return;
}
delete shl.managementToken;
context.response.body = shl;
})
.get('/shl/:shlId/file/:fileIndex', (context) => {
Expand Down
1 change: 1 addition & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface HealthLinkEndpoint {
export interface HealthLinkConfig {
passcode?: string;
exp?: number;
userId?: string;
}

export interface HealthLink {
Expand Down

0 comments on commit 492c9ea

Please sign in to comment.