Skip to content

Commit

Permalink
Merge pull request #77 from f-klubben/api-1.1
Browse files Browse the repository at this point in the history
Implement API 1.1 - Stregsystem
  • Loading branch information
krestenlaust authored Nov 29, 2024
2 parents d634d52 + d04f895 commit 5eca21f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function read_feature_list() {
let config = {
// This uses a cors-proxy (npm run cors-proxy) to add CORS headers
// to stregsystem requests.
base_api_url: process.env.FA_API_URL || "https://localhost:8080/http://localhost:8000/api",
base_api_url: process.env.FA_API_URL || "http://localhost:8000/api",
events_api_url: process.env.FA_EVENTS_API_URL || "https://www.googleapis.com/calendar/v3/calendars/",
events_id: process.env.FA_EVENTS_ID || "[email protected]",
events_api_key: process.env.FA_EVENTS_API_KEY || "AIzaSyBNlYH01_9Hc5S1J9vuFmu2nUqBZJNAXxs",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"source": "pages/index.pug",
"scripts": {
"start": "parcel pages/index.pug pages/songbook/songs/*.pug --dist-dir build/dist",
"build": "parcel build pages/index.pug pages/songbook/songs/*.pug --dist-dir build/dist" ,
"build": "parcel build pages/index.pug pages/songbook/songs/*.pug --dist-dir build/dist",
"cors-proxy": "node cors-proxy.js"
},
"devDependencies": {
Expand All @@ -30,6 +30,7 @@
"process": "^0.11.10",
"punycode": "^1.4.1",
"querystring-es3": "^0.2.1",
"svgo": "^3.3.2",
"typescript": "^4.8.4",
"url": "^0.11.0",
"yargs": "^17.6.2",
Expand Down
6 changes: 3 additions & 3 deletions scripts/stregsystem/api_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {SaleResponse, ActiveProductList} from "./index";

const {base_api_url} = config;

export const get_user_id = (username: string): Promise<number> =>
export const get_member_id = (username: string): Promise<number> =>
fetch(`${base_api_url}/member/get_id?username=${username}`)
.then(res => promise_cond(res.status === 200, res, "Invalid status code"))
.then(res => res.json())
.then(value => value['member_id']);

export const get_user_info = (user_id: number): Promise<any> =>
export const get_member_info = (user_id: number): Promise<any> =>
fetch(`${base_api_url}/member?member_id=${user_id}`)
.then(res => promise_cond(res.status === 200, res, res))
.then(res => res.json());

export const get_user_balance = (user_id: number): Promise<number> =>
export const get_member_balance = (user_id: number): Promise<number> =>
fetch(`${base_api_url}/member/balance?member_id=${user_id}`)
.then(res => promise_cond(res.status === 200, res, res))
.then(res => res.json())
Expand Down
8 changes: 4 additions & 4 deletions scripts/stregsystem/cli_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const decode_html_escapes = (src: string): string => {
return html_decoder.innerText;
}

export const get_user_id = (username: string): Promise<number> => {
export const get_member_id = (username: string): Promise<number> => {
username = JSON.stringify(username);
return py.run(`
if user_mgr.user.username != ${username}:
Expand All @@ -32,7 +32,7 @@ const reg_firstname = /<td>Fornavn\(e\)<\/td>\s*<td>(.+)<\/td>/;
const reg_lastname = /<td>Efternavn<\/td>\s*<td>(.+)<\/td>/;
const reg_balance = /Du har ([0-9.]+) kroner til gode!/;
// sts-cli does not implement a way to get a user from its id
export const get_user_info = (user_id: number): Promise<any> =>
export const get_member_info = (user_id: number): Promise<any> =>
fetch(`${config.base_api_url}/../${config.default_room}/user/${user_id}`)
.if(res => res.status === 200, async res => {
const response_text = await res.text();
Expand All @@ -48,7 +48,7 @@ export const get_user_info = (user_id: number): Promise<any> =>
})
.else_then_promise(() => Promise.reject("Unable to get user info."))

export const get_user_balance = (user_id: number): Promise<number> =>
export const get_member_balance = (user_id: number): Promise<number> =>
py.run(`
if user_mgr.user.user_id != ${user_id}:
user_mgr.user = await user_from_id(${user_id})
Expand Down Expand Up @@ -94,7 +94,7 @@ class CliHelper {

@py.pyFn()
static async user_from_id(id: number) {
const {username, balance} = await get_user_info(id);
const {username, balance} = await get_member_info(id);
return {username, balance, user_id: id};
}

Expand Down
34 changes: 17 additions & 17 deletions scripts/stregsystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export interface SaleResponse {
}

export interface ActiveProductList {
[product_id: string]: [
string, // Product name
number, // Price
]
[product_id: string]: {
name: string; // Product name
price: number; // Price
};
}

type BalanceChange = { old_balance: number, new_balance: number };
Expand All @@ -69,19 +69,19 @@ export interface Backend {
* Gets the id that corresponds to a given username.
* @param username
*/
get_user_id(username: string): Promise<number>;
get_member_id(username: string): Promise<number>;

/**
* Gets the user information associated with the given user id.
* @param user_id
*/
get_user_info(user_id: number): Promise<any>;
get_member_info(user_id: number): Promise<any>;

/**
* Get the current balance of the given user by id.
* @param user_id
*/
get_user_balance(user_id: number): Promise<number>;
get_member_balance(user_id: number): Promise<number>;

/**
* Get a list of products that are active within a given room.
Expand Down Expand Up @@ -137,8 +137,8 @@ export const check_access = (): Promise<AccessStatus> =>
* @param username
*/
export const fetch_profile = async (username: string): Promise<UserProfile> => {
const user_id = await backend.get_user_id(username);
const {name, active, balance} = await backend.get_user_info(user_id);
const user_id = await backend.get_member_id(username);
const {name, active, balance} = await backend.get_member_info(user_id);

return {
username, id: user_id,
Expand Down Expand Up @@ -222,7 +222,7 @@ class FaStregProduct extends HTMLElement {
enable_loading_indicator(true);
try {
await backend.post_sale(`${profile.username} ${this.product_id}`, default_room, profile.id);
const new_balance = await backend.get_user_balance(profile.id);
const new_balance = await backend.get_member_balance(profile.id);
events.profile_balance_change.dispatch({old_balance: profile.balance, new_balance});
} catch (err) {
alert("Purchase failed.");
Expand Down Expand Up @@ -307,7 +307,7 @@ class FaStregCart extends HTMLElement {
enable_loading_indicator(true);
try {
await backend.post_sale(buy_string, default_room, profile.id);
const new_balance = await backend.get_user_balance(profile.id);
const new_balance = await backend.get_member_balance(profile.id);
events.profile_balance_change.dispatch({old_balance: profile.balance, new_balance});
this.contents = {};
this.update();
Expand Down Expand Up @@ -346,7 +346,7 @@ class FaStregCart extends HTMLElement {
*/
compute_total(): number {
return Object.keys(this.contents)
.map(id => this.owner.catalogue[id][1] * this.contents[id])
.map(id => this.owner.catalogue[id].price * this.contents[id])
.reduce(reduce_sum, 0);
}

Expand Down Expand Up @@ -485,10 +485,10 @@ class FaStregCartDialog extends HTMLElement {
const row = document.createElement('tr');

row.append(
text(catalogue[id][0], 'td'), // name
text(catalogue[id].name, 'td'), // name
text(count.toString(), 'td'), // count
text(format_stregdollar(catalogue[id][1]), 'td'), // indv price
text(format_stregdollar(catalogue[id][1] * count), 'td'), // total
text(format_stregdollar(catalogue[id].price), 'td'), // indv price
text(format_stregdollar(catalogue[id].price * count), 'td'), // total
);

return row;
Expand Down Expand Up @@ -575,7 +575,7 @@ class FaProfileWidget extends HTMLElement {
}

async update_balance(now: number) {
const balance = await backend.get_user_balance(this.profile.id);
const balance = await backend.get_member_balance(this.profile.id);
last_balance_bg_update = now;
await AppDatabase.instance.settings.put(now, AppDatabase.balance_update_time_key);
events.profile_balance_change.dispatch({old_balance: this.profile.balance, new_balance: balance})
Expand Down Expand Up @@ -652,7 +652,7 @@ class FaStregsystem extends HTMLElement {

this.catalogue = await backend.get_active_products(default_room);
const product_elements = Object.keys(this.catalogue)
.map(key => new FaStregProduct(this.cart, parseInt(key), ...this.catalogue[key]));
.map(key => new FaStregProduct(this.cart, parseInt(key), this.catalogue[key].name, this.catalogue[key].price));

product_container.append(...product_elements);

Expand Down

0 comments on commit 5eca21f

Please sign in to comment.