Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back end #40

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/components/dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Dashboard() {
}
}, [navigate, userInfo]);

const titleMsg = `مرحباً يا كاتبن ${userInfo?.firstName} ${userInfo?.middleName}`;
const titleMsg = `مرحباً يا كابتن ${userInfo?.firstName} ${userInfo?.middleName}`;

return (
<div className="dashboard">
Expand Down
22 changes: 17 additions & 5 deletions server/controllers/alert.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ const alertController = {
const { id } = req.params
const { sectorBaseName, sectorSuffixName } = req.body

// what if alert is already sent?

let result

result = await db.query(
Expand All @@ -83,6 +81,20 @@ const alertController = {
[id]
)
} else {
// check if sector exist
result = await db.query(
`SELECT EXISTS (
(SELECT 1
FROM "Sector"
WHERE "baseName" = $1 AND
"suffixName" = $2)
) AS exist;`,
[sectorBaseName, sectorSuffixName]
)
if (!result.rows[0].exist) {
return res.status(404).json({ error: 'Sector not found' })
}

// send alert to all captains in sector
result = await db.query(
`INSERT INTO "RecieveNotification" ("notificationId", "captainId", "status")
Expand Down Expand Up @@ -134,7 +146,7 @@ const alertController = {

getAllAlerts: async (req, res) => {
try {
const { status, contentType } = req.body
const { status, contentType } = req.params
const result = await db.query(
`SELECT N.*, R."status"
FROM "Notification" AS N, "RecieveNotification" AS R
Expand All @@ -144,10 +156,10 @@ const alertController = {
)

let alerts = result.rows
if (status) {
if (status !== 'all') {
alerts = alerts.filter((alert) => alert.status === status)
}
if (contentType) {
if (contentType !== 'all') {
alerts = alerts.filter(
(alert) => alert.contentType === contentType
)
Expand Down
128 changes: 128 additions & 0 deletions server/controllers/finance.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,134 @@ const financeController = {
})
}
},

// @desc Get income
// @route GET /api/finance/income
// @access Private
getIncome: async (req, res) => {
try {
const result = await db.query(
`SELECT COALESCE(SUM(value), 0) AS sum FROM "FinanceItem"
WHERE "type" = 'income';`
)
const income = result.rows[0].sum

res.status(200).json({
message: 'Get income successfully',
body: income,
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occurred while getting the income',
})
}
},

// @desc Get expense
// @route GET /api/finance/expense
// @access Private
getExpense: async (req, res) => {
try {
const result = await db.query(
`SELECT COALESCE(SUM(value), 0) AS sum FROM "FinanceItem"
WHERE "type" = 'expense';`
)
const expense = result.rows[0].sum

res.status(200).json({
message: 'Get expense successfully',
body: expense,
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occurred while getting the expense',
})
}
},

// @desc Add a subscription
// @route POST /api/finance/subscription
// @access Private
addSubscription: async (req, res) => {
try {
const { value, sectorBaseName, sectorSuffixName } = req.body

if (req.currentWeek.weekNumber === 0) {
return res.status(400).json({
error: 'No week is currently active',
})
}

let result = await db.query(
`INSERT INTO "FinanceItem" ("value", "timestamp", "type")
VALUES ($1, NOW(), 'income') RETURNING *;`,
[value]
)

const financeItem = result.rows[0]

result = await db.query(
`INSERT INTO "Subscription" ("itemId", "sectorBaseName", "sectorSuffixName", "weekNumber", "termNumber")
VALUES ($1, $2, $3, $4, $5) RETURNING *;`,
[
financeItem.itemId,
sectorBaseName,
sectorSuffixName,
req.currentWeek.weekNumber,
req.currentWeek.termNumber,
]
)

const subscription = result.rows[0]

res.status(200).json({
message: 'Add subscription successfully',
body: { financeItem: financeItem, subscription: subscription },
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occurred while adding subscription',
})
}
},

// @desc Add an other item
// @route POST /api/finance/otherItem
// @access Private
addOtherItem: async (req, res) => {
try {
const { value, type, description } = req.body

let result = await db.query(
`INSERT INTO "FinanceItem" ("value", "timestamp", "type")
VALUES ($1, NOW(), $2) RETURNING *;`,
[value, type]
)

const financeItem = result.rows[0]

result = await db.query(
`INSERT INTO "OtherItem" ("description", "itemId", "generalCaptainId")
VALUES ($1, $2, $3) RETURNING *;`,
[description, financeItem.itemId, req.captain.captainId]
)

const otherItem = result.rows[0]

res.status(200).json({
message: 'Add other item successfully',
body: { financeItem: financeItem, otherItem: otherItem },
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occurred while adding other item',
})
}
},
}

export default financeController
Loading