Skip to content

Instantly share code, notes, and snippets.

@ninjitaru
Created December 24, 2019 09:35
Show Gist options
  • Save ninjitaru/9124fc157363a80b68e7c4c231477d31 to your computer and use it in GitHub Desktop.
Save ninjitaru/9124fc157363a80b68e7c4c231477d31 to your computer and use it in GitHub Desktop.
Get reservation table history
async function printReservation() {
const companyId = '-Kl2_DOjdmBh4Rlj58C1';
const branchId = '-Kl2_DOjdmBh4Rlj58C2';
const db = getCachedAdminDB(companyId);
const tableData = (await db.ref(`branchTables/${companyId}/${branchId}`).once('value')).val();
const tableMap = {};
for (const [sectionId, section] of Object.entries(tableData)) {
for (const [tableId, table] of Object.entries(section.tables)) {
tableMap[tableId] = table;
}
}
const data = {};
// get reservations
const targetStart = new Date("2019-12-31 18:00").valueOf();
const targetEnd = new Date("2019-12-31 20:30").valueOf();
const result = await findRez(branchId, 4000);
for (const hit of result.hits.hits) {
const rez = hit._source;
const rezId = hit._id;
if (rez.reservationType !== ReservationType.Waiting) {
const logs = await findRezLog(rezId);
const timeChanges = [];
const tableChanges = [];
let hasCanceled = false;
_.forEach(logs, log => {
_.forEach(log.changed, change => {
const hasReservationTime = _.includes(change.path, 'reservationTime');
const hasAutoTableIds = _.includes(change.path, 'autoTableIds');
const hasTableIds = _.includes(change.path, 'tableIds');
const canceled = _.includes(change.path, 'canceledTime');
if (!hasCanceled && canceled) {
hasCanceled = log.timestamp;
}
if (hasReservationTime) {
timeChanges.push({ timestamp: log.timestamp, reservationTime: change.rhs });
// console.log(`reservationTime: ${reservationTime.rhs}`);
}
if (hasAutoTableIds || hasTableIds) {
const type = hasAutoTableIds ? 'autoTableIds' : 'tableIds';
// console.log(JSON.stringify(autoTableIds));
if (change.kind === 'D') {
if (_.isBoolean(change.rhs) || _.isBoolean(change.lhs)) {
// console.log(`autoTableIds: delete ${_.last(autoTableIds.path)}`);
tableChanges.push({ timestamp: log.timestamp, type, kind:change.kind, id: _.last(change.path), raw: change });
} else {
// console.log(`autoTableIds: delete ${_.keys(autoTableIds.lhs)}`);
tableChanges.push({ timestamp: log.timestamp, type, kind:change.kind, id: _.keys(change.lhs), raw: change });
}
} else if (change.kind === 'N') {
if (_.isBoolean(change.rhs) || _.isBoolean(change.lhs)) {
tableChanges.push({ timestamp: log.timestamp, type, kind:change.kind, id: _.last(change.path), raw: change });
} else {
tableChanges.push({ timestamp: log.timestamp, type, kind:change.kind, id: _.keys(change.rhs), raw: change });
}
// console.log(`autoTableIds: add ${_.keys(autoTableIds.rhs)}`);
} else {
tableChanges.push({ timestamp: log.timestamp, type, kind:change.kind, id: _.keys(change.rhs), raw: change });
// console.log(`autoTableIds: edited ${_.keys(autoTableIds.rhs)}`);
}
}
});
});
const timeO = _.find(timeChanges, timeO => timeO.reservationTime >= targetStart && targetEnd >= timeO.reservationTime);
if (timeO) {
const shortTime = moment.tz(timeO.reservationTime, 'Asia/Taipei').format('HH:mm');
const hasMoreTime = timeChanges.length > 1;
const startTime = moment.tz(_.first(timeChanges).reservationTime, 'Asia/Taipei').format('YYYY-MM-DD HH:mm');
const endTime = moment.tz(_.last(timeChanges).reservationTime, 'Asia/Taipei').format('YYYY-MM-DD HH:mm');
let canceledTime = '';
if (hasCanceled) {
canceledTime = moment.tz(hasCanceled, 'Asia/Taipei').format('YYYY-MM-DD HH:mm');
}
console.log(`${rezId} ${rez.customer.name} ${shortTime} ${canceledTime} ${hasMoreTime ? ` time from ${startTime}, ${endTime}` : ''}`);
_.forEach(tableChanges, changes => {
const cTime = moment.tz(changes.timestamp, 'Asia/Taipei').format('YYYY-MM-DD HH:mm');
// console.log(JSON.stringify(changes));
let tableString = '';
if (_.isArray(changes.id)) {
tableString = _.reduce(changes.id, (accum, id) => { accum += ` ${id}(${_.get(tableMap, [id, 'name'], 'not found')})`; return accum; }, '');
} else {
tableString = `${changes.id}(${_.get(tableMap, [changes.id, 'name'], 'not found')})`;
}
console.log(`${cTime} ${changes.type} ${changes.kind} ${tableString}`);
});
}
}
}
console.log('done');
}
async function findRezLog(rezId) {
const logs = await es.search({
index: 'reservation-logs-v1',
type: 'reservation-log',
size: 999,
body: {
sort: {
timestamp: { order: 'asc' }
},
query: {
bool: {
must: [
{
range: {
timestamp: {
lte: new Date("2019-12-19 13:00").valueOf(),
}
}
},
{
term: {
'reservationId.keyword': rezId,
}
}
]
}
}
}
});
return _.map(logs.hits.hits, hit => {
return { reservationId: rezId, changed: JSON.parse(hit._source.changed), timestamp: hit._source.timestamp };
});
}
async function findRez(branchId, size = 100) {
return await es.search({
index: 'inline_rez',
type: 'reservation',
size,
body: {
query: {
bool: {
must: [
{
term: { 'branchId.keyword': branchId }
},
{
range: {
createdTime: {
gte: new Date("2019-12-01 00:00").valueOf(),
lte: new Date("2019-12-19 12:57").valueOf(),
}
}
}
]
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment