This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
generated from IgrowkerTraining/template-express-ts
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #85 from igrowker/Alina_Donations
Fixed endpoints and added getMaterialDonationById and updateMaterialD…
- Loading branch information
Showing
7 changed files
with
227 additions
and
112 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 |
---|---|---|
@@ -1,22 +1,91 @@ | ||
import { Request, Response } from 'express'; | ||
import MaterialDonation from '../models/materialDonationModel'; | ||
import { Request, Response } from "express"; | ||
import MaterialDonation from "../models/materialDonationModel"; | ||
|
||
export const createMaterialDonation = async (req: Request, res: Response) => { | ||
try { | ||
const { id, name, quantity, description, materialStatus, donation_request_id } = req.body; | ||
const { | ||
id, | ||
name, | ||
user_id, | ||
quantity, | ||
description, | ||
materialStatus, | ||
donation_request_id, | ||
} = req.body; | ||
|
||
const materialDonation = new MaterialDonation({ | ||
id, | ||
name, | ||
user_id, | ||
quantity, | ||
description, | ||
materialStatus, | ||
donation_request_id, | ||
}); | ||
|
||
await materialDonation.save(); | ||
res.status(201).json({ message: 'Donación de material creada exitosamente', materialDonation }); | ||
res.status(201).json({ | ||
message: "Donación de material creada exitosamente", | ||
materialDonation, | ||
}); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ message: "Error al crear la donación de material", error }); | ||
} | ||
}; | ||
|
||
export const getMaterialDonationsByUser = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
try { | ||
const { user_id } = req.params; | ||
|
||
const materialDonations = await MaterialDonation.find({ user_id }); | ||
|
||
res.status(200).json({ | ||
message: "Donaciones obtenidas exitosamente", | ||
materialDonations, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ message: 'Error al crear la donación de material', error }); | ||
res.status(500).json({ | ||
message: "Error al obtener las donaciones", | ||
error, | ||
}); | ||
} | ||
}; | ||
|
||
export const updateMaterialDonationQuantity = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
try { | ||
const { id } = req.params; | ||
const { quantity } = req.body; | ||
|
||
if (quantity === undefined || quantity < 0) { | ||
return res.status(400).json({ message: "Cantidad inválida" }); | ||
} | ||
|
||
const updatedMaterialDonation = await MaterialDonation.findByIdAndUpdate( | ||
id, | ||
{ quantity }, | ||
{ new: true } | ||
); | ||
|
||
if (!updatedMaterialDonation) { | ||
return res.status(404).json({ message: "Donación no encontrada" }); | ||
} | ||
|
||
res.status(200).json({ | ||
message: "Cantidad actualizada exitosamente", | ||
materialDonation: updatedMaterialDonation, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ | ||
message: "Error al actualizar la cantidad", | ||
error, | ||
}); | ||
} | ||
}; | ||
}; |
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,40 +1,39 @@ | ||
import { Request, Response } from 'express'; | ||
import Stripe from 'stripe'; | ||
import dotenv from 'dotenv' | ||
import { Request, Response } from "express"; | ||
import Stripe from "stripe"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
export const createSession = async (req: Request, res: Response) => { | ||
|
||
const {title, description, moneyAmount, refugee_id, user_id} = req.body; | ||
const stripe = new Stripe(process.env.STRIPE!) | ||
try { | ||
const session = await stripe.checkout.sessions.create({ | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: 'eur', | ||
product_data: { | ||
name: title, | ||
description: description, | ||
}, | ||
unit_amount: moneyAmount, | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
mode: 'payment', | ||
success_url: 'http://localhost:5173/donation-success', | ||
cancel_url: 'http://localhost:5173/donation-cancel', | ||
metadata: { | ||
refugee_id: refugee_id, | ||
user_id: req.user?.id || "unknown_user", | ||
} | ||
}); | ||
const { title, description, moneyAmount, refugee_id, user_id } = req.body; | ||
const stripe = new Stripe(process.env.STRIPE!); | ||
try { | ||
const session = await stripe.checkout.sessions.create({ | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: "eur", | ||
product_data: { | ||
name: title, | ||
description: description, | ||
}, | ||
unit_amount: moneyAmount, | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
mode: "payment", | ||
success_url: "http://localhost:5173/donation-success", | ||
cancel_url: "http://localhost:5173/donation-cancel", | ||
metadata: { | ||
refugee_id: refugee_id, | ||
user_id: req.user?.id || "unknown_user", | ||
}, | ||
}); | ||
|
||
// Devolver la sesión creada | ||
res.status(201).json({ message: 'Session created successfully', session }); | ||
} catch (error) { | ||
console.error('Error creando la sesión:', error); | ||
res.status(500).json({ message: 'Error creating session', error }); | ||
} | ||
}; | ||
// Devolver la sesión creada | ||
res.status(201).json({ message: "Session created successfully", session }); | ||
} catch (error) { | ||
console.error("Error creando la sesión:", error); | ||
res.status(500).json({ message: "Error creating session", error }); | ||
} | ||
}; |
Oops, something went wrong.