-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ElyasMehraein/dev
Dev
- Loading branch information
Showing
90 changed files
with
4,389 additions
and
1,891 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"recommendations": [ | ||
"dalirnet.rtl-markdown" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,90 @@ | ||
import connectToDB from '@/configs/db' | ||
import connectToDB from '@/configs/db'; | ||
import { cookies } from "next/headers"; | ||
import { verifyToken } from "@/controllers/auth"; | ||
import { notFound } from 'next/navigation' | ||
import { notFound } from 'next/navigation'; | ||
import BillModel from '@/models/Bill'; | ||
import BusinessModel from '@/models/Business' | ||
import Business from '@/components/templates/business/business' | ||
import UserModel from '@/models/User' | ||
import Profile from '@/components/templates/Profile/Profile' | ||
import { redirect } from 'next/navigation' | ||
import { POST } from '../api/auth/logout/route'; | ||
import BusinessModel from '@/models/Business'; | ||
import Business from '@/components/templates/business/business'; | ||
import UserModel from '@/models/User'; | ||
import Profile from '@/components/templates/Profile/Profile'; | ||
import { redirect } from 'next/navigation'; | ||
import { GET } from '../api/auth/logout/route'; | ||
import BusinessRelationModel from '@/models/BusinessRelation'; | ||
|
||
export default async function subDirectory({ params }) { | ||
|
||
try { | ||
const token = cookies().get("token")?.value; | ||
const tokenPayLoad = verifyToken(token); | ||
|
||
connectToDB() | ||
let logedUserCode = null; | ||
connectToDB(); | ||
let logedUser = null; | ||
|
||
if (tokenPayLoad) { | ||
logedUserCode = JSON.parse(JSON.stringify(await UserModel.findOne( | ||
logedUser = JSON.parse(JSON.stringify(await UserModel.findOne( | ||
{ _id: tokenPayLoad.id }, | ||
"-_id code" | ||
))).code; | ||
"-_id code businesses" | ||
).populate([ | ||
"businesses", | ||
]))); | ||
} | ||
|
||
if (isNaN(params.subDirectory)) { | ||
|
||
// Find Business with relationships (populated) | ||
const business = JSON.parse(JSON.stringify(await BusinessModel.findOne({ | ||
businessName: params.subDirectory | ||
}).populate("workers"))); | ||
}).populate([ | ||
"workers", | ||
"guild", | ||
]))); | ||
|
||
if (!business) { | ||
console.log("business not found in DB"); | ||
notFound() | ||
notFound(); | ||
} | ||
|
||
// Find relevant Bills | ||
const bills = JSON.parse(JSON.stringify(await BillModel.find({ | ||
from: business._id, | ||
isAccept: true | ||
}).populate("to"))); | ||
|
||
// Get related businesses for logged-in user (if any) | ||
let relations = JSON.parse(JSON.stringify(await BusinessRelationModel.find({ | ||
$or: [ | ||
{ provider: business._id }, | ||
{ receiver: business._id }, | ||
], | ||
}).populate("receiver provider"))); | ||
|
||
return ( | ||
<Business business={business} | ||
logedUserCode={logedUserCode} | ||
<Business | ||
business={business} | ||
logedUser={logedUser} | ||
bills={bills} | ||
relations={relations} | ||
/> | ||
) | ||
|
||
} | ||
|
||
); | ||
|
||
} else { | ||
|
||
const user = JSON.parse(JSON.stringify(await UserModel.findOne( | ||
{ code: params.subDirectory }, | ||
).populate("businesses"))) | ||
const user = JSON.parse(JSON.stringify(await UserModel.findOne( | ||
{ code: params.subDirectory }, | ||
).populate("businesses"))); | ||
|
||
if (!user) { | ||
console.log("user not found in DB"); | ||
notFound() | ||
if (!user) { | ||
console.log("user not found in DB"); | ||
notFound(); | ||
} | ||
return ( | ||
<Profile user={user} | ||
logedUser={logedUser} | ||
/> | ||
); | ||
} | ||
return ( | ||
<Profile user={user} | ||
logedUserCode={logedUserCode} | ||
/> | ||
) | ||
} catch (err) { | ||
console.error('server error in subDirectory:', err); | ||
POST() | ||
redirect("/w"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import UserModel from '@/models/User'; | ||
import BusinessModel from '@/models/Business'; | ||
import BillModel from '@/models/Bill'; | ||
import { GET } from "@/app/api/auth/me/route" | ||
import GuildModel from '@/models/Guild'; | ||
|
||
export async function PUT(req) { | ||
const body = await req.json() | ||
let { billId } = body | ||
const response = await GET(req) | ||
const user = await response.json() | ||
|
||
try { | ||
const bill = await BillModel.findById(billId); | ||
if (!bill) { | ||
throw new Error('document not found'); | ||
} | ||
if (bill.to.toString() !== user._id.toString()) { | ||
return Response.json( | ||
{ message: `You are not authorized to update the bill ` }, | ||
{ status: 403 } | ||
) | ||
} | ||
if (bill.status !== "pending") { | ||
return Response.json( | ||
{ message: "its not pending bill" }, | ||
{ status: 400 } | ||
) | ||
} | ||
let guild = await GuildModel.findById(bill.guild); | ||
for (let product of bill.products) { | ||
let theGuildProduct = guild.products.find(guildProduct => guildProduct.productName === product.productName); | ||
if (!theGuildProduct) { | ||
guild.products.push({ | ||
productName: product.productName, | ||
unitOfMeasurement: product.unitOfMeasurement, | ||
}) | ||
await guild.save() | ||
} | ||
} | ||
let business = await BusinessModel.findById(bill.from); | ||
|
||
for (let product of bill.products) { | ||
let theBusinessProduct = business.deliveredProducts.find(businessProduct => businessProduct.productName === product.productName); | ||
let uniqueCustomer = await BillModel.distinct('to', { | ||
productName: bill.productName, | ||
from: bill.from, | ||
}); | ||
if (theBusinessProduct) { | ||
theBusinessProduct.totalDelivered += product.amount; | ||
theBusinessProduct.thisYearDelivered += product.amount | ||
theBusinessProduct.uniqueCustomer = uniqueCustomer.length | ||
await business.save() | ||
|
||
} else { | ||
business.deliveredProducts.push({ | ||
productName: product.productName, | ||
unitOfMeasurement: product.unitOfMeasurement, | ||
totalDelivered: product.amount, | ||
lastYearDelivered: 0, | ||
thisYearDelivered: product.amount, | ||
uniqueCustomer: 1, | ||
}); | ||
await business.save() | ||
} | ||
} | ||
bill.status = "accepted"; | ||
await bill.save(); | ||
console.log(`the bill updated successfully.`); | ||
return Response.json( | ||
{ message: `the bill updated successfully.` }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error(`Error updating the bill:`, error); | ||
Response.json( | ||
{ message: `Error updating the bill `, error }, | ||
{ status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { cookies } from "next/headers"; | ||
|
||
export async function POST(req) { | ||
export async function GET(req) { | ||
cookies().delete("token"); | ||
return Response.json({ message: "User sign out !!" },{status: 200}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.