Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyperdana committed Jul 28, 2020
0 parents commit 9621740
Show file tree
Hide file tree
Showing 12 changed files with 1,932 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.log
Empty file added README.md
Empty file.
175 changes: 175 additions & 0 deletions fkrtl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@

var
mongoDB = require("mongodb"),
_ = require('lodash'),
moment = require('moment'),
lastToken = '',
cekToken = token => token === lastToken,
kodepoli = require('./kodepoli')['kodepoli'],
tindakan = require('./tindakan')['tindakan'],
refpoli = ['INT', 'ANA', 'OBG', 'BED', 'GIG'],
withThis = (obj, cb) => cb(obj),

startOfTheDay = timestamp => +moment(
moment(timestamp).format('YYYY-MM-DD')
),

randomId = () =>
[1, 1].map(() =>
Math.random().toString(36).slice(2)
).join(''),

dbCall = action => mongoDB.MongoClient.connect(
process.env.atlas,
{useNewUrlParser: true, useUnifiedTopology: true},
(err, client) => [action(client.db(process.env.dbname))]
),

response = obj => ({
response: obj, metadata: {message: 'Ok', code: 200}
}),

errorResponder = (cb, text) => cb.send(response({
error: text || 'data atau token tidak valid, perbaiki'
})),

validityCheck = array => array.filter(i => !i[0]).map(i => i[1]),

jadwalOperasi = (pasien, rawat, item) =>
({
kodebooking: item.idtindakan.substring(0, 8),
tanggaloperasi: moment(item.jadwal).format('YYYY-MM-DD'),
jenistindakan: tindakan.find(
v => v._id === item.idtindakan
).nama,
kodepoli: refpoli[rawat.klinik-1],
namapoli: kodepoli[refpoli[rawat.klinik-1]],
terlaksana: 0,
nopeserta: pasien.identitas.bpjs,
lastupdate: +moment()
}),

fkrtl = (req, res) => ({
getToken: () => withThis(validityCheck([
[req.body.username === process.env.username, 'username salah'],
[req.body.password === process.env.password, 'password salah']
]), errors => !errors.length ? withThis(
randomId(), token => [
lastToken = token,
res.send(response({token: token}))
]
) : errorResponder(res, errors.join())),

getNoAntrean: () => withThis(validityCheck([
[cekToken(req.headers['x-token']), 'token salah'],
[req.body.nomorkartu, 'nomor kartu salah'],
[+moment(req.body.tanggalperiksa) > startOfTheDay(+moment()), 'tanggal periksa harus pada hari lain'],
[+moment(req.body.tanggalperiksa).day(), 'hanya hari kerja saja'],
[kodepoli[req.body.kodepoli], 'kode poli salah'],
[[1, 2].includes(req.body.jenisreferensi), 'jenis referensi salah'],
[[1, 2].includes(req.body.jenisrequest), 'jenis request salah']
]), errors => !errors.length ? withThis(
randomId(), kodebooking =>
dbCall(db =>
db.collection('queue').find({
nomorkartu: req.body.nomorkartu,
tanggalperiksa: req.body.tanggalperiksa
}).toArray((err, arr) => arr.length === 0 ? dbCall(
db => db.collection('queue').countDocuments({
tanggalperiksa: req.body.tanggalperiksa
})
.then(number => db.collection('queue').insertOne({
_id: kodebooking,
no_antrian: 'R'+(number+1),
nomorkartu: req.body.nomorkartu,// 0000000000000123
nik: req.body.nik, // 1471071611890001
notelp: req.body.notelp, // 08117696000
tanggalperiksa: req.body.tanggalperiksa, // "2019-12-11"
kodepoli: req.body.kodepoli, // "001", panduan ada di gSheets
nomorreferensi: req.body.nomorreferensi, // "0001R0040116A000001"
jenisreferensi: req.body.jenisreferensi, // 1, {1: rujukan, 2: kontrol}
jenisrequest: req.body.jenisrequest, // 2, {1: pendaftaran, 2: poli}
polieksekutif: req.body.polieksekutif, // 2, {1: eksekutif, 2: reguler}
timestamp: Date.now()
}, (fail, ok) => res.send(response({
nomorantrean: 'R'+(number+1), kodebooking,
jenisantrean: +req.body.jenisrequest,
estimasidilayani: +moment(req.body.tanggalperiksa),
namapoli: kodepoli[req.body.kodepoli],
namadokter: ''
}))))
) : errorResponder(res, 'sudah booking pada hari tersebut')
)
)
) : errorResponder(res, errors.join())),

getRekapAntrean: () =>
withThis(validityCheck([
[cekToken(req.headers['x-token']), 'token salah'],
[kodepoli[req.body.kodepoli], 'kode poli salah'],
[+moment(req.body.tanggalperiksa), 'tanggal periksa salah']
]), errors => !errors.length ? withThis([
{timestamp: {$gt: startOfTheDay(
+moment(req.body.tanggalperiksa)
)}},
{kodepoli: req.body.kodepoli}
], conds => dbCall(
db => db.collection('queue').countDocuments({$and: conds})
.then(totalantrean => db.collection('queue').countDocuments(
{$and: conds.concat([{done: true}])}
).then(jumlahterlayani => res.send(response({
namapoli: kodepoli[req.body.kodepoli],
totalantrean, jumlahterlayani,
lastupdate: !moment()
}))))
)) : errorResponder(res, errors.join())),

getKodeBookingOperasi: () =>
withThis(validityCheck([
[cekToken(req.headers['x-token']), 'token salah'],
[req.body.nopeserta, 'nomor peserta kosong']
]), errors => !errors.length ? dbCall(
db => db.collection('patients')
.findOne({'identitas.bpjs': req.body.nopeserta})
.then(i => res.send(response({list:
_.flattenDeep(([]).concat(
i.rawatJalan || [],
i.emergency || []
).map(
j => j.soapDokter && j.soapDokter.tindakan &&
j.soapDokter.tindakan.map(
k => k.jadwal && jadwalOperasi(i, j, k)
)
)).filter(l => l)
})))
) : errorResponder(res, errors.join())),

getJadwalOperasi: () =>
withThis(validityCheck([
[cekToken(req.headers['x-token']), 'token salah'],
[
+moment(req.body.tanggalawal) < +moment(req.body.tanggalakhir),
'tanggal awal harus lebih kecil dari tanggal akhir'
]
]), errors => !errors.length ? dbCall(
db => db.collection('patients').find({}).toArray()
.then(array => res.send(response({list:
_.flattenDeep(array.map(i => ([]).concat(
i.rawatJalan || [],
i.emergency || [],
).map(
j => j.soapDokter && j.soapDokter.tindakan &&
j.soapDokter.tindakan.map(k =>
k.jadwal && [
k.jadwal > +moment(req.body.tanggalawal),
k.jadwal < +moment(req.body.tanggalakhir)
].every(Boolean) &&
jadwalOperasi(i, j, k)
).filter(l => l)
).filter(l => l)))
})))
) : errorResponder(res, errors.join()))

}[req.params.api]())

exports.fkrtl = fkrtl
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var express = require('express'),
bodyParser = require('body-parser'),
fkrtl = require('./fkrtl')['fkrtl'],
kamar = require('./kamar')['kamar'],

app = express()
.use((req, res, next) => [
req.headers['content-type'] = 'application/json',
next()
])
.use(bodyParser.json())
.post('/fkrtl/:api', fkrtl)
.post('/kamar/:api', kamar)
.use(express.static('public'))

var server = require('http').Server(app).listen(3000)
146 changes: 146 additions & 0 deletions kamar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
var mongoDB = require("mongodb"),
_ = require('lodash'),
CronJob = require('cron').CronJob,
CryptoJS = require('crypto-js'),
fetch = require('node-fetch'),
withThis = (obj, cb) => cb(obj),
base_url = process.env.stage === 'production' ?
process.env.base_url_prod
: process.env.base_url_dev,

timestamp = () =>
Math.floor(Date.now()/1000).toString(),

encrypt = (data, secret) =>
CryptoJS.enc.Base64.stringify(
CryptoJS.HmacSHA256(data, secret)
),

signature = () => encrypt(
[process.env.cons_id, '&', timestamp()].join(''),
process.env.secret_key
),

dbCall = action => mongoDB.MongoClient.connect(
process.env.atlas,
{useNewUrlParser: true, useUnifiedTopology: true},
(err, client) => err ? console.log(err)
: action(client.db(process.env.dbname))
),

beds = {
vip: {tarif: 350, kamar: {tulip: 1, bougenvil: 1, sakura: 1}},
kl3: {tarif: 200, kamar: {kenanga: 2, cempaka: 2, claudia: 2, ferbia: 2, yasmin: 2, edelwise: 2}},
kl2: {tarif: 150, kamar: {seroja: 4, mawar: 2, dahlia: 2, lili: 2, zahara: 2, matahari: 4}},
kl1: {tarif: 100, kamar: {anggrek: 4, teratai: 8, kertas: 4, melati: 4}}
},

headers = () => ({
'Content-Type': 'application/json',
'X-cons-id': process.env.cons_id,
'X-timestamp': timestamp(),
'X-signature': signature()
}),

autoUpdateKamar = (new CronJob(
'0 0 */3 * * *', // per 3 jam
() => dbCall(db =>
db.collection('patients').find({
rawatInap: {$elemMatch: {
keluar: {$exists: false}
}}
}).toArray().then(res =>
withThis(
{
used: _.flattenDeep(res.map(
i => i.rawatInap
.filter(j => !j.keluar)
.map(j => j.bed)
)).filter(Boolean)
.reduce((res, inc) => _.assign(res, {
[inc.kamar]: (res[inc.kamar] || 0) + 1
}), {}),
bedList: _.flattenDepth(
_.map(beds, (a, b) =>
_.map(a, c => _.map(c, (d, e) =>
({kelas: b, kamar: e, kapasitas: c[e]})
))
)
, 2)
},
({used, bedList}) =>
bedList.reduce((res, inc) =>
[
...res,
_.includes(_.keys(used), inc.kamar) ? {
kodekelas: inc.kelas,
koderuang: inc.kamar,
namaruang: _.startCase(inc.kamar),
kapasitas: inc.kapasitas,
tersedia: (inc.kapasitas - used[inc.kamar])
} : {}
]
, []).filter(i => _.keys(i).length)
.map(i =>
fetch(
[base_url, 'bed/update/', process.env.kode_ppk].join(''),
{
method: 'post',
body: JSON.stringify(i),
headers: headers()
}
)
.then(res => res.json())
.then(res => res && console.log(res))
.catch(console.log))
)
)
)
)).start(),

kamar = (req, res) => ({
referensi_kelas: () =>
fetch(base_url+'ref/kelas/')
.then(data => data.json())
.then(data => data && res.send(data)),
update_ketersediaan: () =>
fetch(
[base_url, 'bed/update/', process.env.kode_ppk].join(''),
{
method: 'post',
body: JSON.stringify(req.body),
headers: headers()
}
)
.then(data => data.json())
.then(data => data && res.send(data)),
ketersediaan_kamar: () =>
fetch(
[base_url, 'bed/read/', process.env.kode_ppk, '/1/100'].join(''),
{headers: headers()}
)
.then(data => data.json())
.then(data => data && res.send(data)),
ruangan_baru: () =>
fetch(
[base_url, 'bed/create/', process.env.kode_ppk].join(''),
{
method: 'post', headers: headers(),
body: JSON.stringify(req.body)
}
)
.then(data => data.json())
.then(data => data && res.send(data)),
hapus_ruangan: () =>
fetch(
[base_url, 'bed/delete/', process.env.kode_ppk].join(''),
{
method: 'post', headers: headers(),
body: JSON.stringify(req.body)
}
)
.then(res => res.json())
.then(data => data && res.send(data)),
})[req.params.api]()

exports.kamar = kamar
Loading

0 comments on commit 9621740

Please sign in to comment.