Make received messages dates more dynamic

This commit is contained in:
Mikołaj Pich 2022-10-27 23:14:33 +02:00
parent aec541ff35
commit 8321043cdd
2 changed files with 14 additions and 4 deletions

View file

@ -1,7 +1,7 @@
const express = require('express');
const router = express.Router();
const protocol = require('../utils/connection');
const {timestampToIsoTzFormat} = require('../utils/converter');
const {timestampToIsoTzFormat, dateToTimestamp} = require('../utils/converter');
const {fromString} = require('uuidv4');
router.get("/", (req, res) => {
@ -38,12 +38,17 @@ router.get([
"/api/Odebrane",
"/api/OdebraneSkrzynka",
], (req, res) => {
res.json(require("../../data/api/messages/WiadomosciOdebrane").map(item => {
const currentTimestamp = dateToTimestamp(new Date());
res.json(require("../../data/api/messages/WiadomosciOdebrane").map((item, i) => {
let itemTimestamp = item.DataWyslaniaUnixEpoch;
if (i < 7) {
itemTimestamp = currentTimestamp - (i * i * 3600 * 6);
}
return {
"apiGlobalKey": fromString(item.WiadomoscId.toString()),
"korespondenci": item.Nadawca + " - P - (123456)",
"temat": item.Tytul,
"data": timestampToIsoTzFormat(item.DataWyslaniaUnixEpoch),
"data": timestampToIsoTzFormat(itemTimestamp),
"skrzynka": "Jan Kowalski - U - (123456)",
"hasZalaczniki": true,
"przeczytana": !!item.GodzinaPrzeczytania,

View file

@ -1,4 +1,4 @@
const { addDays, toDate, format} = require('date-fns');
const {addDays, toDate, format, getTime} = require('date-fns');
const WEEK_TICK = 6048000000000;
const DAY_TICK = 864000000000;
@ -36,6 +36,10 @@ function timestampToIsoTzFormat(timestamp) {
return format(new Date(timestamp * 1000), 'yyyy-MM-dd\'T\'HH:mm:ss.SSXXX');
}
function dateToTimestamp(date) {
return getTime(date) / 1000;
}
function getMonday(date) {
let day = date.getDate() - date.getDay() + 1;
return new Date(date.getFullYear(), date.getMonth(), day);
@ -96,3 +100,4 @@ exports.getPrevWeekTick = getPrevWeekTick;
exports.getNextWeekTick = getNextWeekTick;
exports.formatDate = formatDate;
exports.timestampToIsoTzFormat = timestampToIsoTzFormat;
exports.dateToTimestamp = dateToTimestamp;