Skip to content

Commit

Permalink
Implemented guests cache. Implemented guests function within unifi.js…
Browse files Browse the repository at this point in the history
…. Updated app screenshots. Updated manifest.json. Added details.ejs component. Implemented voucher detail slide-over. Updated cache.js logic to fetch guests. Updated README.md. Implemented voucher detail component route.
  • Loading branch information
glenndehaan committed Apr 25, 2024
1 parent 72d6f4d commit f68ee13
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 12 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A small UniFi Voucher Site for simple voucher creation

[![Image Size](https://img.shields.io/docker/image-size/glenndehaan/unifi-voucher-site)](https://hub.docker.com/r/glenndehaan/unifi-voucher-site)

![Vouchers Overview - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/2bc18cce-afb2-41e9-a34c-d923fab9f6e4)
![Vouchers Overview - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/b0d5c208-2ac7-444e-977d-31287ff19e8b)

## Structure

Expand Down Expand Up @@ -233,22 +233,28 @@ The application will automatically format the voucher for 80mm paper width, ensu
## Screenshots

### Login (Desktop)
![Login - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/89289053-50e0-4169-9916-2bce7191bf49)
![Login - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/5f89ecbd-7e03-4fd0-ae7d-279d16321384)

### Vouchers Overview (Desktop)
![Vouchers Overview - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/2bc18cce-afb2-41e9-a34c-d923fab9f6e4)
![Vouchers Overview - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/b0d5c208-2ac7-444e-977d-31287ff19e8b)

### Create Voucher (Desktop)
![Create Voucher - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/32803e88-c3cb-4708-914c-7e7184eae1c6)
![Create Voucher - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/72f8dcf0-6642-4c89-849f-21cfbcc488ab)

### Voucher Details (Desktop)
![Voucher Details - Desktop](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/b84ad74c-afaa-4bf1-8bc1-398fb0450ff1)

### Login (Mobile)
![Login - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/c236bb65-1ef5-41dd-976f-8f72a98416a3)
![Login - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/d74bc487-5b80-4bb6-8617-da870cdf4cec)

### Vouchers Overview (Mobile)
![Voucher Overview - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/68656bad-b4a2-495d-baae-61a64ed52e72)
![Voucher Overview - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/c986e03d-5edf-4b04-8903-0b42ff1c4fc9)

### Create Voucher (Mobile)
![Create Voucher - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/b393c3d9-1e87-40e7-8998-9607872b161f)
![Create Voucher - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/f1cef8c8-a7a5-4238-8a2e-461835375f29)

### Voucher Details (Mobile)
![Voucher Details - Mobile](https://github.com/glenndehaan/unifi-voucher-site/assets/7496187/28b8f97b-8042-4e6d-b1dc-8386860a1e39)

## License

Expand Down
3 changes: 2 additions & 1 deletion modules/cache.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* Internal application cache
*
* @type {{vouchers: *[], updated: number}}
* @type {{guests: *[], vouchers: *[], updated: number}}
*/
module.exports = {
vouchers: [],
guests: [],
updated: 0
};
46 changes: 46 additions & 0 deletions modules/unifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,52 @@ const unifiModule = {
reject(e);
});
});
},

/**
* Returns a list with all UniFi Guests
*
* @param retry
* @return {Promise<unknown>}
*/
guests: (retry = true) => {
return new Promise((resolve, reject) => {
startSession().then(() => {
controller.getGuests().then((guests) => {
log.info(`[UniFi] Found ${guests.length} guest(s)`);
resolve(guests);
}).catch((e) => {
log.error('[UniFi] Error while getting guests!');
log.debug(e);

// Check if token expired, if true attempt login then try again
if (e.response) {
if(e.response.status === 401 && retry) {
log.info('[UniFi] Attempting re-authentication & retry...');

controller = null;
unifiModule.guests(false).then((e) => {
resolve(e);
}).catch((e) => {
reject(e);
});
} else {
// Something else went wrong lets clear the current controller so a user can retry
log.error(`[UniFi] Unexpected ${JSON.stringify({status: e.response.status, retry})} cleanup controller...`);
controller = null;
reject('[UniFi] Error while getting guests!');
}
} else {
// Something else went wrong lets clear the current controller so a user can retry
log.error('[UniFi] Unexpected cleanup controller...');
controller = null;
reject('[UniFi] Error while getting guests!');
}
});
}).catch((e) => {
reject(e);
});
});
}
}

Expand Down
Binary file modified public/images/screenshots/desktop_screenshot_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/screenshots/desktop_screenshot_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/screenshots/desktop_screenshot_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/screenshots/mobile_screenshot_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/screenshots/mobile_screenshot_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/screenshots/mobile_screenshot_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/screenshots/mobile_screenshot_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"type": "image/png",
"form_factor": "narrow"
},
{
"src": "./images/screenshots/mobile_screenshot_4.png",
"sizes": "413x877",
"type": "image/png",
"form_factor": "narrow"
},
{
"src": "./images/screenshots/desktop_screenshot_1.png",
"sizes": "1280x720",
Expand All @@ -55,6 +61,12 @@
"sizes": "1280x720",
"type": "image/png",
"form_factor": "wide"
},
{
"src": "./images/screenshots/desktop_screenshot_4.png",
"sizes": "1280x720",
"type": "image/png",
"form_factor": "wide"
}
],
"start_url": "/",
Expand Down
36 changes: 34 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,21 @@ if(webService) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
});

if(vouchers) {
log.info('[Cache] Requesting UniFi Guests...');

const guests = await unifi.guests().catch((e) => {
log.error('[Cache] Error requesting guests!');
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
});

if(vouchers && guests) {
cache.vouchers = vouchers;
cache.guests = guests;
cache.updated = new Date().getTime();
log.info(`[Cache] Saved ${vouchers.length} voucher(s)`);
log.info(`[Cache] Saved ${guests.length} guest(s)`);

res.cookie('flashMessage', JSON.stringify({type: 'info', message: 'Synced Vouchers!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
res.cookie('flashMessage', JSON.stringify({type: 'info', message: 'Synced Vouchers & Guests!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
}

return;
Expand All @@ -401,6 +410,29 @@ if(webService) {
updated: cache.updated
});
});
app.get('/voucher/:id', [authorization.web], async (req, res) => {
const voucher = cache.vouchers.find((e) => {
return e._id === req.params.id;
});
const guests = cache.guests.filter((e) => {
return e.voucher_id === req.params.id;
});

if(voucher) {
res.render('components/details', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
timeConvert: time,
bytesConvert: bytes,
voucher,
guests
});
} else {
res.status(404);
res.render('404', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''
});
}
});
}

if(apiService) {
Expand Down
Loading

0 comments on commit f68ee13

Please sign in to comment.