Skip to content

Commit fd7f0b5

Browse files
author
crrochon
committed
Added Models + Updated Elders
Changed all Clients to Elders, and created the PhoneNumbers and Status models
1 parent e5f467f commit fd7f0b5

File tree

8 files changed

+230
-32
lines changed

8 files changed

+230
-32
lines changed

server/controllers/ElderController.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const db = require('../models')
2+
const { Op } = require('sequelize')
3+
const logger = require('../logger/logger')
4+
5+
// TODO - Make an Elder Object, and quit destructuring manually you animal. Ideally --> 'const elder = req.body'
6+
// TODO - Provide input cleaning for Create and Edit Elders...
7+
// -> These should use a suite of functions we can use everywhere
8+
// ! -> For now, trust the user....
9+
10+
const methods = {
11+
12+
// Destrtucture ID from request and call query with that ID
13+
async getElderById(req, res) {
14+
const cId = req.query.id
15+
16+
if (!cId) return res.status(403).json({ status: "failed", message: "Invalid request." })
17+
18+
const elder = await db.Elders.findOne({
19+
attributes: ['*'], // Force 'SELECT *' for the arbitrary indexing
20+
where: { elderId: cId },
21+
raw: true, // trims garbage meta from SQL
22+
})
23+
24+
return res.status(200).json({ status: "success", elder })
25+
},
26+
27+
// Simply grab all Elders, also return total count
28+
async getAllElders(req, res) {
29+
const elders = await db.Elders.findAll({ attributes: ['*'], raw: true })
30+
const count = await db.Elders.count({ raw: true })
31+
return res.status(200).json({ status: "success", count, elders })
32+
},
33+
34+
// Take in the fields for an elder, if the elder exists, update it, otherwise create it
35+
async createElder(req, res) {
36+
const firstName = req.body.firstName
37+
const lastName = req.body.lastName
38+
const fullName = firstName + ' ' + lastName // Yeet
39+
const notes = req.body.notes
40+
const phoneNumber = req.body.phoneNumber
41+
const address = req.body.address
42+
const mobility = req.body.mobility
43+
const numOfCancels = 0 // We just made them...
44+
const createdDate = new Date().toISOString().slice(0, 23).replace('T', ' ') // Format to sqlserver datetime object
45+
46+
const elder = await db.Elders.create({
47+
firstName: firstName,
48+
lastName: lastName,
49+
fullName: fullName,
50+
address: address,
51+
phoneNumber: phoneNumber,
52+
mobility: mobility,
53+
notes: notes,
54+
numOfCancels: numOfCancels,
55+
createdAt: createdDate,
56+
updatedAt: createdDate
57+
})
58+
59+
return res.status(200).json({ message: "success", elder })
60+
},
61+
62+
// Take in the fields for a elder, if the edler exists, update it
63+
async editElder(req, res) {
64+
const elderId = req.body.elderId
65+
const firstName = req.body.firstName
66+
const lastName = req.body.lastName
67+
const fullName = req.body.fullName
68+
const address = req.body.address
69+
const phoneNumber = req.body.phoneNumber
70+
const mobility = req.body.mobility
71+
const notes = req.body.notes
72+
const numOfCancels = req.body.numOfCancels
73+
const updatedAt = new Date().toISOString().slice(0, 23).replace('T', ' ') // Format to sqlserver datetime object
74+
75+
// If user sucks at giving us data, die gracefully
76+
if (!elderId)
77+
return res.status(403).json({ message: "error", message: "User provided no elder id..." })
78+
79+
// If we can't find the elder to edit, die gracefully
80+
if (! (await db.Elders.findOne({
81+
attributes: ["elderId"],
82+
where: { elderId: elderId },
83+
raw: true
84+
}))
85+
) return res.status(403).json({ message: "error", message: "Elder doesn't exist." })
86+
87+
const success = await db.Elders.update({ // returns boolean iff success
88+
firstName: firstName,
89+
lastName: lastName,
90+
fullName: fullName,
91+
address: address,
92+
phoneNumber: phoneNumber,
93+
mobility: mobility,
94+
notes: notes,
95+
numOfCancels: numOfCancels,
96+
updatedAt: updatedAt
97+
},
98+
{ where: { elderId: elderId } }
99+
)
100+
101+
if (success) return res.status(200).json({ message: "success", message: "Elder Updated." })
102+
return res.status(403).json({ message: "error", message: "Something went wrong. Check fields and try again." })
103+
},
104+
105+
// Full send --> kill anything you tell us to
106+
async deleteElder(req, res) {
107+
const dId = req.body.elderId
108+
await db.Elders.destroy({ where: { elderId: dId } })
109+
return res.status(200).json({ message: "success", data: "Elder deleted." })
110+
},
111+
}
112+
113+
module.exports = methods

server/models/Appointments.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ module.exports = (sequelize, DataTypes) => {
7272
alias: "Driver ID",
7373
aliasValue: "driverid",
7474
},
75-
clientId: {
75+
elderId: {
7676
type: DataTypes.INTEGER,
77-
field: "clientId",
77+
field: "elderId",
7878
searchable: true,
79-
alias: "Client ID",
80-
aliasValue: "clientid",
79+
alias: "Elder ID",
80+
aliasValue: "elderid",
8181
},
8282
isCancelled: {
8383
type: DataTypes.BOOLEAN,

server/models/Client.js renamed to server/models/Elder.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module.exports = (sequelize, DataTypes) => {
22
return sequelize.define(
3-
'Clients',
3+
'Elders',
44
{
5-
clientId: {
5+
elderId: {
66
type: DataTypes.INTEGER,
77
allowNull: false,
8-
field: "clientId",
8+
field: "elderId",
99
primaryKey: true,
1010
autoIncrement: true,
1111
searchable: true,
12-
alias: "Client ID",
13-
aliasValue: "clientId",
12+
alias: "Elder ID",
13+
aliasValue: "elderId",
1414
},
1515

1616
firstName: {
@@ -88,7 +88,7 @@ module.exports = (sequelize, DataTypes) => {
8888
},
8989
},
9090
{
91-
tableName: "Clients",
91+
tableName: "Elders",
9292
timestamps: false,
9393
},
9494
)

server/models/PhoneNumbers.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module.exports = (sequelize, DataTypes) => {
2+
return sequelize.define(
3+
'Phone Numbers',
4+
{
5+
phoneNumberId:{
6+
type: DataTypes.STRING,
7+
allowNull: false,
8+
field: 'phoneNumberId',
9+
primaryKey: true,
10+
autoIncrement: true,
11+
searchable: true,
12+
alias: 'Phone Number ID',
13+
aliasValue: 'phoneNumberId',
14+
},
15+
16+
referenceId:{
17+
type: DataTypes.STRING,
18+
allowNull: false,
19+
field: 'referenceId',
20+
primaryKey: true,
21+
autoIncrement: true,
22+
searchable: true,
23+
alias: 'Reference ID',
24+
aliasValue: 'referenceId',
25+
},
26+
27+
referenceType:{
28+
29+
},
30+
31+
phoneNumber:{
32+
type: DataTypes.STRING,
33+
allowNull: false,
34+
field: 'phoneNumber',
35+
primaryKey: true,
36+
autoIncrement: true,
37+
searchable: true,
38+
alias: 'Phone Number',
39+
aliasValue: 'phoneNumber',
40+
},
41+
42+
phoneExtension:{
43+
type: DataTypes.INTEGER,
44+
allowNull: false,
45+
field: 'phoneExtension',
46+
primaryKey: true,
47+
autoIncrement: true,
48+
searchable: true,
49+
alias: 'Phone Extension',
50+
aliasValue: 'phoneExtension',
51+
},
52+
53+
description:{
54+
type: DataTypes.ARRAY,
55+
allowNull: false,
56+
field: 'descrption',
57+
primaryKey: false,
58+
autoIncrement: true,
59+
searchable: true,
60+
alias: 'Description',
61+
aliasValue: 'description',
62+
},
63+
64+
order:{
65+
type: DataTypes.STRING,
66+
allowNull: false,
67+
field: 'order',
68+
primaryKey: false,
69+
autoIncrement: true,
70+
searchable: true,
71+
alias: 'Order',
72+
aliasValue: 'order',
73+
},
74+
},
75+
{
76+
tableName: "Phone Numbers",
77+
timeStamps: false,
78+
}
79+
)
80+
};

server/models/Status.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (sequelize, DataTypes) => {
2+
return sequelize.define(
3+
'Status'
4+
)
5+
}

server/routes/client.routes.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

server/routes/elder.routes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
const ElderController = require('../controllers/ElderController.js')
4+
5+
const baseUrl = 'elders';
6+
7+
// Get Elder by ID
8+
router.get(`/${baseUrl}/`, ElderController.getElderById)
9+
10+
// Get All Elder
11+
router.get(`/${baseUrl}/list/`, ElderController.getAllElders)
12+
13+
// Create Elder
14+
router.post(`/${baseUrl}/create`, ElderController.createElder)
15+
16+
// Edit Elder
17+
router.post(`/${baseUrl}/edit`, ElderController.editElder)
18+
19+
// Delete Elder
20+
router.post(`/${baseUrl}/delete`, ElderController.deleteElder)
21+
22+
module.exports = router
File renamed without changes.

0 commit comments

Comments
 (0)