Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
clr-li committed Apr 5, 2024
2 parents 946a1c2 + ee13074 commit baf91e7
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 61 deletions.
18 changes: 9 additions & 9 deletions public/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function setRecordSettings() {

document.getElementById('require-join').addEventListener('change', async e => {
const requireJoin = e.target.checked ? 1 : 0;
await PUT(`/businesses${getBusinessId()}/settings/requirejoin?new=${requireJoin}`);
await PUT(`/businesses/${getBusinessId()}/settings/requirejoin?new=${requireJoin}`);
});

function validateEventTime(startDate, endDate, startTime, endTime, isRepeating = false) {
Expand Down Expand Up @@ -217,8 +217,8 @@ document.getElementById('submitevent').addEventListener('click', () => {
const starttime = document.getElementById('starttime').value;
const enddate = document.getElementById('enddate').value;
const endtime = document.getElementById('endtime').value;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime() / 1000;
const endtimestamp = new Date(enddate + 'T' + endtime).getTime() / 1000;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime();
const endtimestamp = new Date(enddate + 'T' + endtime).getTime();
const isRepeating = document.getElementById('repeat').checked;
const allTags = document.getElementById('all-tags');

Expand All @@ -242,7 +242,7 @@ document.getElementById('submitevent').addEventListener('click', () => {
if (isRepeating) {
const frequency = document.getElementById('frequency').value.toLowerCase();
const interval = document.getElementById('interval').value;
const timezoneOffsetMS = new Date().getTimezoneOffset() * 60 * 1000;
const timezoneOffsetMS = new Date().getTimezoneOffset() * 60_000;
let daysoftheweek = [];
let counter = 0;
if (interval < 1) {
Expand Down Expand Up @@ -322,8 +322,8 @@ function getEventData() {
GET(`/businesses/${getBusinessId()}/events/${getEventId()}`).then(res =>
res.json().then(eventinfo => {
tagHTMLString(eventinfo.tag);
var startDate = new Date(eventinfo.starttimestamp * 1000);
var endDate = new Date(eventinfo.endtimestamp * 1000);
var startDate = new Date(+eventinfo.starttimestamp);
var endDate = new Date(+eventinfo.endtimestamp);
document.getElementById('eventdetails').innerHTML = /* html */ `
<label for="update-name">Name:</label><br>
<input style="min-width: var(--max-width-medium);" type="text" value="${sanitizeText(
Expand Down Expand Up @@ -409,7 +409,7 @@ function getEventData() {

const startdate = document.getElementById('update-startdate').value;
const starttime = document.getElementById('update-starttime').value;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime() / 1000;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime();

DELETE(
`/businesses/${getBusinessId()}/events/${getEventId()}?repeatEffect=${repeatEffect}&starttimestamp=${starttimestamp}&repeatId=${
Expand Down Expand Up @@ -478,8 +478,8 @@ function getEventData() {
const starttime = document.getElementById('update-starttime').value;
const enddate = document.getElementById('update-enddate').value;
const endtime = document.getElementById('update-endtime').value;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime() / 1000;
const endtimestamp = new Date(enddate + 'T' + endtime).getTime() / 1000;
const starttimestamp = new Date(startdate + 'T' + starttime).getTime();
const endtimestamp = new Date(enddate + 'T' + endtime).getTime();
const starttimedelta = starttimestamp - eventinfo.starttimestamp;
const endtimedelta = endtimestamp - eventinfo.endtimestamp;
const allTags = document.getElementById('current-tags');
Expand Down
4 changes: 2 additions & 2 deletions public/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ for (const [i, business] of Object.entries(businesses)) {
});
business['events'] = [];
for (const event of events) {
const startDate = new Date(event.starttimestamp * 1000);
const endDate = new Date(event.endtimestamp * 1000);
const startDate = new Date(+event.starttimestamp);
const endDate = new Date(+event.endtimestamp);
const status = event.status ?? (Date.now() > endDate.getTime() ? 'ABSENT' : null);
let edit = '<br>';
const starttime = startDate.toLocaleTimeString(undefined, {
Expand Down
2 changes: 1 addition & 1 deletion public/components/CircleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CircleLoader extends HTMLElement {
// Create HTML for this component
const html = htmlToElements(/* html */ `
<div class="center">
<div class="tip"><p style="font-size: 18px;"><span style="color: var(--secondary); font-size: 22px; font-weight: 1000">&#9432;&#160;&#160;</span>${
<div class="tip"><p style="font-size: 18px;"><span style="color: var(--secondary); font-size: 22px">&#9432;&#160;&#160;</span>${
tips[randomTip]
}</p></div>
</div>
Expand Down
17 changes: 6 additions & 11 deletions public/components/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ export class Table extends Component {
}
// Event headers
for (let i = 0; i < events.length; i++) {
var startDate = new Date(events[i].starttimestamp * 1000);
var endDate = new Date(events[i].endtimestamp * 1000);
var startDate = new Date(+events[i].starttimestamp);
var endDate = new Date(+events[i].endtimestamp);
let eventName = events[i].name || '[Unnamed]';
html += `<th style="min-width: max-content; text-wrap: nowrap;" class="cell" data-time="${sanitizeText(
events[i].starttimestamp,
Expand Down Expand Up @@ -227,7 +227,7 @@ export class Table extends Component {
if (statusColor[records[k].status]) {
color = statusColor[records[k].status];
}
let recordTimestamp = new Date(records[k].timestamp * 1000)
let recordTimestamp = new Date(+records[k].timestamp)
.toString()
.split(' ')
.slice(1, 5)
Expand All @@ -249,8 +249,7 @@ export class Table extends Component {
}
}
if (!statusupdate) {
const status =
Date.now() > parseInt(events[j].endtimestamp) * 1000 ? 'ABSENT' : 'N/A';
const status = Date.now() > parseInt(events[j].endtimestamp) ? 'ABSENT' : 'N/A';
if (statusColor[status]) {
color = statusColor[status];
}
Expand Down Expand Up @@ -555,18 +554,14 @@ export class Table extends Component {
showCell &&
new Date(
this.shadowRoot.getElementById('filterstart').value + 'T00:00',
).getTime() /
1000 <
cell.dataset.time;
).getTime() < cell.dataset.time;
}
if (showend) {
showCell =
showCell &&
new Date(
this.shadowRoot.getElementById('filterend').value + 'T23:59',
).getTime() /
1000 >
cell.dataset.time;
).getTime() > cell.dataset.time;
}
if (eventName) {
showCell = showCell && cell.dataset.name === eventName;
Expand Down
6 changes: 3 additions & 3 deletions public/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function handleBusinessLoad(business) {
dayOfTheWeekCounts[status] = [0, 0, 0, 0, 0, 0, 0];
}
for (const event of userdata.userEvents) {
const start = new Date(event.starttimestamp * 1000);
const start = new Date(+event.starttimestamp);
const status = event.status || (start <= now ? 'ABSENT' : 'N/A');
statusCounts[status]++;
dayOfTheWeekCounts[status][start.getDay()]++;
Expand Down Expand Up @@ -172,14 +172,14 @@ async function handleBusinessLoad(business) {
${
userdata.userEvents
.sort((a, b) => a.starttimestamp - b.starttimestamp)
.filter(ev => ev.starttimestamp * 1000 >= now)
.filter(ev => ev.starttimestamp >= now)
.slice(0, 10)
.map(
ev =>
'<li>' +
sanitizeText(ev.name) +
' - ' +
sanitizeText(new Date(ev.starttimestamp * 1000).toDateString()) +
sanitizeText(new Date(+ev.starttimestamp).toDateString()) +
'</li>',
)
.join('') || 'No upcoming events'
Expand Down
6 changes: 3 additions & 3 deletions public/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function canCreateEvent(role) {

async function autoCreateEvent() {
const date = new Date(); // current date and time
const starttimestamp = date.getTime() / 1000;
const endtimestamp = starttimestamp + 3600; // 1 hour
const starttimestamp = date.getTime();
const endtimestamp = starttimestamp + 3_600_000; // add 1 hour
const name = date.toDateString();
const description = 'Auto generated event created on ' + date.toString();

Expand Down Expand Up @@ -88,7 +88,7 @@ const eventInfo = await res.json();

const { get: getStatus, set: setStatus } = useURL(
'status',
Date.now() <= parseInt(eventInfo.starttimestamp) * 1000 ? 'PRESENT' : 'LATE',
Date.now() <= parseInt(eventInfo.starttimestamp) ? 'PRESENT' : 'LATE',
);
const statusSelector = document.getElementById('status');
statusSelector.addEventListener('select', e => {
Expand Down
29 changes: 11 additions & 18 deletions public/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ await requireLogin();

const { get: getBusinessId } = await initBusinessSelector('business-selector', async () => {
const res = await GET(
`/businesses/${getBusinessId()}/attendance/statuscounts?tag=&role=&start=&end=${Math.round(
Date.now() / 1000,
)}`,
`/businesses/${getBusinessId()}/attendance/statuscounts?tag=&role=&start=&end=${Date.now()}`,
);
const memberAttArr = await res.json();
const numRes = await GET(
`/businesses/${getBusinessId()}/events/count?tag=&start=&end=${Math.round(
Date.now() / 1000,
)}`,
`/businesses/${getBusinessId()}/events/count?tag=&start=&end=${Date.now()}`,
);
const numPastEvents = (await numRes.json())['total_count'];
runMemberStatsTable(memberAttArr, numPastEvents);
Expand Down Expand Up @@ -219,7 +215,7 @@ document.getElementById('show-filter').onclick = async () => {
document.getElementById('submit-filterform').onclick = async () => {
let start = document.getElementById('filter-start').value;
let end = document.getElementById('filter-end').value;
start = new Date(start + 'T00:00').getTime() / 1000;
start = new Date(start + 'T00:00').getTime();
const month = new Date(Date.now()).getMonth() + 1;
let monthString = month.toString();
if (month < 10) monthString = '0' + month;
Expand All @@ -231,19 +227,18 @@ document.getElementById('show-filter').onclick = async () => {
'-' +
new Date(Date.now()).getDate()
) {
end =
new Date(
end + 'T' + new Date().getHours() + ':' + new Date().getMinutes(),
).getTime() / 1000;
end = new Date(
end + 'T' + new Date().getHours() + ':' + new Date().getMinutes(),
).getTime();
} else {
end = new Date(end + 'T23:59').getTime() / 1000;
end = new Date(end + 'T23:59').getTime();
}
if (isNaN(start)) start = '';
if (isNaN(end)) end = Math.round(Date.now() / 1000);
if (isNaN(end)) end = Math.round(Date.now());
if (end != '' && start != '' && end < start) {
Popup.alert('End date must be after start date', 'var(--error)');
return;
} else if (end > Math.round(Date.now() / 1000) || start > Math.round(Date.now() / 1000)) {
} else if (end > Math.round(Date.now()) || start > Math.round(Date.now())) {
Popup.alert('Start and end date must be before today', 'var(--error)');
return;
}
Expand Down Expand Up @@ -272,13 +267,11 @@ document.getElementById('show-filter').onclick = async () => {
};

const res = await GET(
`/businesses/${getBusinessId()}/attendance/statuscounts?tag=&role=&start=&end=${Math.round(
Date.now() / 1000,
)}`,
`/businesses/${getBusinessId()}/attendance/statuscounts?tag=&role=&start=&end=${Date.now()}`,
);
const memberAttArr = await res.json();
const numRes = await GET(
`/businesses/${getBusinessId()}/events/count?tag=&start=&end=${Math.round(Date.now() / 1000)}`,
`/businesses/${getBusinessId()}/events/count?tag=&start=&end=${Date.now()}`,
);
const numPastEvents = (await numRes.json())['total_count'];
runMemberStatsTable(memberAttArr, numPastEvents);
Expand Down
4 changes: 2 additions & 2 deletions public/util/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export async function initEventSelector(id, getBusinessId, onselect, onupdate) {
const events = await res.json();
const eventNames = new Set();
const options = events.map(event => {
const startDate = new Date(event.starttimestamp * 1000);
const endDate = new Date(event.endtimestamp * 1000);
const startDate = new Date(+event.starttimestamp);
const endDate = new Date(+event.endtimestamp);
eventNames.add(event.name);
const option = document.createElement('option');
option.value = event.name + ' (' + event.id + ')';
Expand Down
12 changes: 5 additions & 7 deletions server/Attendance.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ router.post('/businesses/:businessId/events/:eventId/attendance', async (request

await db().run(
...SQL`INSERT INTO Records (event_id, business_id, user_id, timestamp, status)
VALUES (${eventId}, ${businessId}, ${userId}, ${Math.round(Date.now() / 1000)}, ${status})`,
VALUES (${eventId}, ${businessId}, ${userId}, ${Date.now()}, ${status})`,
);
response.sendStatus(200);
});
Expand All @@ -80,7 +80,7 @@ router.patch('/businesses/:businessId/events/:eventId/attendance', async (reques
for (const id of ids.split(',')) {
await db().run(
...SQL`INSERT INTO Records(status, business_id, event_id, user_id, timestamp)
VALUES (${status}, ${businessId}, ${eventId}, ${id}, ${Math.round(Date.now() / 1000)})`,
VALUES (${status}, ${businessId}, ${eventId}, ${id}, ${Date.now()})`,
);
}
response.sendStatus(200);
Expand Down Expand Up @@ -111,16 +111,14 @@ router.put(
response.status(400).send('Attendance already recorded');
return;
}
if (existingRecord && parseInt(existingRecord.endtimestamp) * 1000 < Date.now()) {
if (existingRecord && parseInt(existingRecord.endtimestamp) < Date.now()) {
response.status(400).send('Can only alter attendance for future events');
return;
}

await db().run(
...SQL`INSERT INTO Records(status, business_id, event_id, user_id, timestamp)
VALUES ('ABSENT(self-marked)', ${businessId}, ${eventId}, ${uid}, ${Math.round(
Date.now() / 1000,
)})`,
VALUES ('ABSENT(self-marked)', ${businessId}, ${eventId}, ${uid}, ${Date.now()})`,
);

response.sendStatus(200);
Expand Down Expand Up @@ -263,7 +261,7 @@ router.post('/businesses/:businessId/events/:eventId/attendance/me', async (requ

await db().run(
...SQL`INSERT INTO Records (event_id, business_id, user_id, timestamp, status)
VALUES (${eventId}, ${businessId}, ${uid}, ${Math.round(Date.now() / 1000)}, ${status})`,
VALUES (${eventId}, ${businessId}, ${uid}, ${Date.now()}, ${status})`,
);
response.sendStatus(200);
});
Expand Down
10 changes: 5 additions & 5 deletions server/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ function createEventSequence(
db().run(
...SQL`INSERT INTO Events (business_id, name, description, starttimestamp, endtimestamp, repeat_id, tag)
VALUES (${businessId}, ${name}, ${description}, ${
(current.getTime() + timezoneOffsetMS) / 1000
}, ${(currentEndDate.getTime() + timezoneOffsetMS) / 1000}, ${repeatId}, ${tag})`,
current.getTime() + timezoneOffsetMS
}, ${currentEndDate.getTime() + timezoneOffsetMS}, ${repeatId}, ${tag})`,
);
if (frequency === 'daily') current.setDate(current.getDate() + interval);
else if (frequency === 'weekly') current.setDate(current.getDate() + 7 * interval);
Expand Down Expand Up @@ -182,9 +182,9 @@ router.post('/businesses/:businessId/events/recurring', async function (request,
const name = request.query.name;
const description = request.query.description;
const timezoneOffsetMS =
parseInt(request.query.timezoneOffsetMS) - new Date().getTimezoneOffset() * 60 * 1000; // actual offset includes server offset
const startDate = new Date(parseInt(request.query.starttimestamp) * 1000 - timezoneOffsetMS);
const endDate = new Date(parseInt(request.query.endtimestamp) * 1000 - timezoneOffsetMS);
parseInt(request.query.timezoneOffsetMS) - new Date().getTimezoneOffset() * 60_000; // actual offset includes server offset
const startDate = new Date(request.query.starttimestamp - timezoneOffsetMS);
const endDate = new Date(request.query.endtimestamp - timezoneOffsetMS);
const frequency = request.query.frequency;
const interval = parseInt(request.query.interval);
const daysoftheweek = request.query.daysoftheweek;
Expand Down

0 comments on commit baf91e7

Please sign in to comment.