Skip to content

Commit 5f96374

Browse files
authored
Merge pull request #7 from Zefanrakh/presencelist
Presencelist
2 parents 6c8e6bb + 61800c8 commit 5f96374

32 files changed

+5963
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

app.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const express = require("express");
2+
const app = express();
3+
const PORT = process.env.PORT || 3000;
4+
const router = require("./routes");
5+
const errorHandler = require("./middlewares/errorHandler");
6+
const cors = require("cors");
7+
8+
app.use(cors());
9+
app.use(express.urlencoded({ extended: true }));
10+
app.use(express.json());
11+
app.use(router);
12+
app.use(errorHandler);
13+
14+
module.exports = app;

bin/http

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var http = require('http');
2+
var app = require('../app')
3+
const PORT = process.env.PORT || 3001
4+
app.listen(PORT, ()=> {
5+
console.log(`Listening on port ${PORT}`);
6+
})

config/config.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"development": {
3+
"username": "postgres",
4+
"password": "123",
5+
"database": "daycare",
6+
"host": "127.0.0.1",
7+
"dialect": "postgres"
8+
},
9+
"test": {
10+
"username": "postgres",
11+
"password": 123,
12+
"database": "testing_daycare",
13+
"host": "127.0.0.1",
14+
"dialect": "postgres"
15+
},
16+
"production": {
17+
"username": "root",
18+
"password": null,
19+
"database": "database_production",
20+
"host": "127.0.0.1",
21+
"dialect": "mysql"
22+
}
23+
}

config/fbServiceAccountKey.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "daycare-f2982",
4+
"private_key_id": "1ab3a7496047263d562504a882b3d8a869e8cd0b",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDZOmuLtmZz8bGE\nZIBjXTMx9sKjL9bDLX5JgqqhoJt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ChEiV4FnbGMH\nIE/nCQexR7cH32AnPSE9boBO+mN/kSp6aH6293jyJDSWwHFkEh8Vu1/zdpH/kZvR\n3ax5GERQu7G+GEQ7a/KBqtR/6qIkJZwrrxTkdcgw9y7ktJ5nYk40i3l/AoGBAJj8\nHyxx+viQtXhrHOidJIhAEFvUSGNS+MJmSoLvSHbXEWoRSKzWk/3x1chcsYEnflpR\n+ByJ5QSGUJluJMKExyUb/av2yFXs/xZjd5MEuf+b0r9kQyKcccIG1xsKQ2t4cDhb\nDlcufwZ2gQ8iJZspdPXSXp+B58WWsgbOiLZSEdH3AoGBAKDYMkxTsQFXdhj8emPI\nyKvfxgPcRyiQKydaY1wOADNlfYh1eSQb5n9QAuPY+RYhrF20cz+CBjK1Nv8+bsJW\nmdOA2V6l7xWQhKyIUmFH5wYkLe8wBxVsUIeWrIYVKyE0WpGM48P2Jg68nqLbzrkE\nH4Hp7T6vEhmlVzGFOcLK8F4U\n-----END PRIVATE KEY-----\n",
6+
"client_email": "firebase-adminsdk-5ac2z@daycare-f2982.iam.gserviceaccount.com",
7+
"client_id": "105069740328546554913",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-5ac2z%40daycare-f2982.iam.gserviceaccount.com"
12+
}

controllers/UserController.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { Customer } = require("../models");
2+
3+
class UserController {
4+
static register(req, res, next) {
5+
const { name, picture, email, phoneNumber, address } = req.user;
6+
Customer.create({
7+
name,
8+
email,
9+
picture,
10+
phoneNumber,
11+
address,
12+
})
13+
.then((user) => {
14+
res.status(201).json(user);
15+
})
16+
.catch((err) => {
17+
next(err);
18+
});
19+
}
20+
21+
static login(req, res, next) {
22+
const { name, picture, email, phoneNumber, address } = req.user;
23+
Customer.findOne({
24+
where: {
25+
email,
26+
},
27+
})
28+
.then((user) => {
29+
res.status(200).json(user);
30+
})
31+
.catch((err) => {
32+
next(err);
33+
});
34+
}
35+
}
36+
37+
module.exports = UserController;

controllers/appointmentController.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const { Appointment, Customer, Price } = require('../models')
2+
3+
class Controller {
4+
static async getAppointment(req, res, next){
5+
try {
6+
let appointments
7+
if (req.query.status){
8+
appointments = await Appointment.findAll({
9+
where: {status: req.query.status},
10+
include: {model: Customer}
11+
})
12+
} else {
13+
appointments = await Appointment.findAll({ include: {model: Customer} })
14+
}
15+
console.log(appointments);
16+
res.status(200).json({message: 'read data success', data: appointments})
17+
} catch (error) {
18+
next(error);
19+
}
20+
}
21+
22+
static async postAppointment(req, res, next){
23+
try {
24+
console.log(req.body);
25+
const {CustomerId, childName, childAge, startDate, endDate, status, childCategory, packageCategory, quantity, note} = req.body
26+
const price = await Price.findOne({where: {category: childCategory, package: packageCategory}})
27+
const total = price.price * quantity
28+
const PriceId = price.id
29+
const insertedData = await Appointment.create({CustomerId, childName, childAge, startDate, endDate, status, PriceId, quantity, total, note})
30+
res.status(201).json(insertedData)
31+
} catch (error) {
32+
console.log(error);
33+
next(error);
34+
}
35+
}
36+
37+
//gak dipake
38+
static async getAppointmentById(req, res, next){
39+
try {
40+
const appointment = await Appointment.findOne({where: {id: req.params.id}, include: {model: Customer}})
41+
if(appointment){
42+
res.status(200).json(appointment)
43+
}else{
44+
next({status:404, msg: 'data not found'})
45+
}
46+
} catch (error) {
47+
next(error);
48+
}
49+
}
50+
51+
static async getAppointmentByCustomerId(req, res, next){
52+
try {
53+
const appointment = await Appointment.findAll({where: {Customerid: req.params.Customerid}})
54+
if(appointment){
55+
res.status(200).json(appointment)
56+
}else{
57+
next({status: 404, msg: 'data not found'})
58+
}
59+
} catch (error) {
60+
next(error);
61+
}
62+
}
63+
64+
static async patchAppointment(req, res, next){
65+
const { status } = req.body
66+
try {
67+
let updatedData = await Appointment.update({ status: status }, {
68+
where: {
69+
id: req.params.id
70+
},
71+
returning: true
72+
})
73+
if(updatedData){
74+
res.status(200).json(updatedData)
75+
}else{
76+
res.status(400).json({message: 'Data not found'})
77+
}
78+
} catch (error) {
79+
next(error);
80+
}
81+
}
82+
}
83+
84+
module.exports = Controller

controllers/customerController.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const {Customer} = require('../models/index')
2+
3+
class CustomerController{
4+
static getCustomer(req,res,next){// next digunakan menyesuaikan error handler
5+
Customer.findAll()
6+
.then(customers=>{
7+
res.status(200).json({customers})
8+
})
9+
.catch(err=>{
10+
next(err)
11+
})
12+
}
13+
14+
static postCustomer(req,res,next){
15+
let createCustomer={
16+
name:req.body.name,
17+
address:req.body.address,
18+
email:req.body.email,
19+
phoneNumber:req.body.phoneNumber
20+
}
21+
Customer.create(createCustomer)
22+
.then(customer=>{
23+
res.status(201).json({customer})
24+
}).catch(err=>{
25+
console.log(err);
26+
next(err)
27+
})
28+
}
29+
30+
static getCustomerId(req, res, next){
31+
let id = req.params.id
32+
Customer.findByPk(id)
33+
.then(customer=>{
34+
if(!customer){
35+
res.status(404).json({message:err.message})
36+
}else{
37+
res.status(200).json({customer})
38+
}
39+
})
40+
.catch(err=>{
41+
next(err)
42+
})
43+
}
44+
45+
static deleteCustomer(req,res,next){
46+
let id = req.params.id
47+
Customer.destroy({where:{id}})
48+
.then(customer=>{
49+
if(!customer){
50+
res.status(404).json({message:err.message})
51+
}else{
52+
res.status(200).json({message:"Customer Deleted"})
53+
}
54+
})
55+
.catch(err=>{
56+
next(err)
57+
})
58+
}
59+
}
60+
61+
62+
63+
module.exports = CustomerController

controllers/presence.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const { PresenceList, Appointment } = require('../models')
2+
3+
class Controller {
4+
static async postPresence(req, res, next) {
5+
try {
6+
const { dropperName, pickupperName, pickupTime, AppointmentId } = req.body
7+
const presenceDate = new Date()
8+
const insertedData = await PresenceList.create({dropperName, pickupperName, pickupTime, presenceDate, AppointmentId })
9+
res.status(201).json(insertedData)
10+
} catch (error) {
11+
console.log(error);
12+
next(error)
13+
}
14+
}
15+
16+
static async getPresence(req, res, next) {
17+
try {
18+
const presenceList = await PresenceList.findAll({include: {model: Appointment}})
19+
res.status(200).json(presenceList)
20+
} catch (error) {
21+
next(error)
22+
}
23+
}
24+
25+
static async getPresenceById() {
26+
try {
27+
const presenceList = await PresenceList.findOne({ where: {id: req.params.id}, include: {model: Appointment}})
28+
if(presenceList){
29+
res.status(200).json(presenceList)
30+
}else{
31+
next({status: 404, msg: 'data not found'})
32+
}
33+
} catch (error) {
34+
next(error)
35+
}
36+
}
37+
38+
static async getPresenceByCustomerId() {
39+
try {
40+
const presenceList = await Appointment.findOne({ where: {CustomerId: req.params.CustomerId}, include: {model: Presence}})
41+
if(presenceList){
42+
res.status(200).json(presenceList)
43+
}else{
44+
next({status: 404, msg: 'data not found'})
45+
}
46+
} catch (error) {
47+
next(error)
48+
}
49+
}
50+
}
51+
52+
module.exports = Controller

firebase/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var admin = require("firebase-admin");
2+
3+
var serviceAccount = require("../config/fbServiceAccountKey.json");
4+
5+
admin.initializeApp({
6+
credential: admin.credential.cert(serviceAccount),
7+
});
8+
9+
module.exports = admin;

middlewares/auth.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const admin = require("../firebase");
2+
3+
const authCheck = async (req, res, next) => {
4+
// console.log(req.headers); // token
5+
try {
6+
const decodedIdToken = await admin
7+
.auth()
8+
.verifyIdToken(req.headers.authtoken);
9+
// console.log("FIREBASE USER IN AUTHCHECK", decodedIdToken);
10+
console.log(decodedIdToken.picture);
11+
req.user = decodedIdToken;
12+
next();
13+
} catch (err) {
14+
res.status(401).json({
15+
err: "Invalid or expired token",
16+
});
17+
}
18+
};
19+
20+
module.exports = authCheck;

middlewares/errorHandler.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const errorHandler = async (err, req, res, next) => {
2+
if (err.status) {
3+
res.status(err.status).json({ message: err.msg });
4+
} else if (err.errors) {
5+
let message = err.errors.map((el) => el.message);
6+
res.status(400).json({ message });
7+
} else if (err.raw) {
8+
res.json({ message: err.raw.message });
9+
} else if (err.code) {
10+
res.status(err.code).json({ message: err.msg });
11+
} else {
12+
res.status(500).json({ message: "Internal Server Error" });
13+
}
14+
};
15+
16+
module.exports = errorHandler;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
module.exports = {
3+
up: async (queryInterface, Sequelize) => {
4+
await queryInterface.createTable("Customers", {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: Sequelize.INTEGER,
10+
},
11+
name: {
12+
allowNull: false,
13+
type: Sequelize.STRING,
14+
},
15+
address: {
16+
allowNull: false,
17+
type: Sequelize.STRING,
18+
},
19+
email: {
20+
allowNull: false,
21+
type: Sequelize.STRING,
22+
unique: true,
23+
},
24+
phoneNumber: {
25+
allowNull: false,
26+
type: Sequelize.STRING,
27+
},
28+
UserId: {
29+
type: Sequelize.INTEGER,
30+
},
31+
createdAt: {
32+
allowNull: false,
33+
type: Sequelize.DATE,
34+
},
35+
updatedAt: {
36+
allowNull: false,
37+
type: Sequelize.DATE,
38+
},
39+
});
40+
},
41+
down: async (queryInterface, Sequelize) => {
42+
await queryInterface.dropTable("Customers");
43+
},
44+
};

0 commit comments

Comments
 (0)