Skip to content

Instantly share code, notes, and snippets.

@Bishwas-py
Created March 23, 2024 14:42
Show Gist options
  • Save Bishwas-py/cacd32a241ba35625b039af43c3fa363 to your computer and use it in GitHub Desktop.
Save Bishwas-py/cacd32a241ba35625b039af43c3fa363 to your computer and use it in GitHub Desktop.
redundant task in github
export async function charge_per_action(event: RequestEvent,
usage_title: string | null = null,
required_credit_amount = 2): Promise<MessageOut | null> {
if (!usage_title) {
usage_title = event.url.pathname;
}
try {
const response = await event.fetch(`$api/payout/charge/`, {
method: 'POST',
headers: event.locals.fetchAuthHeaders,
body: JSON.stringify({ usage_title })
});
if (response.ok) {
return null;
}
return await response.json();
} catch (e) {
console.log(e);
return {
'message': 'An error occurred while processing your charge request.',
'message_type': 'error',
'alias': 'error'
};
}
}
export function charge(action: (event: RequestEvent, charge_action: typeof charge_per_action) => Promise<MessageOut | unknown>, required_credit_amount = 2) {
return async function(event: RequestEvent): Promise<ActionFailure<MessageOut> | Promise<unknown>> {
if (!event.locals.user) {
return fail(401, {
message: 'You need to be logged in to perform this action.',
message_type: 'error',
alias: 'login'
});
}
if ((event.locals.credits_info?.total_available_credits_amount || 0) < required_credit_amount) {
return fail(402, {
message: `You need at least ${required_credit_amount} credits to perform this action.`,
message_type: 'error',
alias: 'credits',
action: {
path: '/credits',
label: 'Get some credits!'
}
});
}
return action(event, charge_per_action);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment