-
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.
payment and subscription models added
- Loading branch information
Roger Calaf
authored and
Roger Calaf
committed
Dec 23, 2024
1 parent
aa67f32
commit b17e3f0
Showing
9 changed files
with
241 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const User = require('../model/User'); | ||
const Subscription = require('../model/Subscription'); | ||
const { updateEmployee } = require('./employeesController'); | ||
|
||
|
||
const getAllSubscriptions = async (req, res) => { | ||
const subscriptions = await Subscription.find(); | ||
if (!subscriptions) return res.status(204).json({ 'message': 'No subscriptions found' }); | ||
res.json(subscriptions); | ||
} | ||
|
||
|
||
const getSubscription = async (req, res) => { | ||
console.log(req?.params?.id) | ||
if (!req?.params?.id) return res.status(400).json({ "message": 'Subscription ID required' }); | ||
const subscription = await Subscription.findOne({ _id: req.params.id }).exec(); | ||
if (!subscription) { | ||
return res.status(204).json({ 'message': `Subscription ID ${req.params.id} not found` }); | ||
} | ||
res.json(subscription); | ||
} | ||
|
||
const getUserSubscriptions = async (req, res) => { | ||
if (!req?.params?.id) return res.status(400).json({ "message": 'User ID required' }); | ||
//console.log(req?.query?.page); | ||
//TODO: add pagination filter + Date filter | ||
const limit = 5; | ||
let options = {} | ||
if(req?.query?.page){ | ||
//let page = (!req?.query?.page ? req?.query?.page : 1); | ||
options = { limit: limit, skip: limit*(req.query.page-1), sort: [{"date": "desc" }] }; | ||
}else{ | ||
options = { sort: [{"date": "desc" }] }; | ||
} | ||
console.log(options) | ||
const user = await User.findOne({ _id: req.params.id }).populate({path:'subscriptions',options}).exec(); | ||
if (!user) return res.status(204).json({ 'message': 'No users found' }); | ||
const subscriptions = user.subscriptions; | ||
// console.log(observations); | ||
res.json(subscriptions); | ||
} | ||
|
||
const createNewSubscription = async (req, res) => { | ||
// console.log('This is a new oservation body received...') | ||
console.log(req.body); | ||
console.log(req.params.id); | ||
|
||
let user = await User.findOne({ _id: req.params.id }); | ||
if (!user) return res.status(204).json({ 'message': 'No users found' }); | ||
req.body.user = user; | ||
console.log(req.body); | ||
try { | ||
const result = await Subscription.create(req.body); | ||
// console.log(req.body.observationTypes.snowpack); | ||
user.subscriptions.push(result.toObject({ getters: true })); | ||
user.pro = true; | ||
user.expiresAt = req.body.expiresAt; | ||
let saveResult = await user.save(); | ||
// console.log(saveResult) | ||
return res.status(201).json({'subscriptions': user.subscriptions, 'subscriptionId': result._id}); | ||
} catch (err) { | ||
console.log('error') | ||
console.error(err); | ||
res.status(500).json({ 'message': 'Error creating subscription' }); | ||
} | ||
} | ||
|
||
const editSubscription = async (req, res) =>{ | ||
const { id } = req.params; | ||
//console.log('------------edit user form body -------') | ||
console.log(req.body); | ||
console.log(id); | ||
if (!id) return res.status(400).json({ "message": 'Subscription ID required' }); | ||
// console.log(id); | ||
let subscription = await Subscription.findOne({ _id: id }).exec(); | ||
if (!subscription) { | ||
return res.status(204).json({ 'message': `Subscription ID ${id} not found` }); | ||
} | ||
|
||
subscription = {...subscription,...req.body}; | ||
subscription.save(); | ||
|
||
try{ | ||
// console.log('user saved') | ||
let response = await subscription.save(); | ||
|
||
// console.log("----received from the app---"); | ||
res.status(200).json(subscription); | ||
}catch (e){ | ||
console.log(e); | ||
res.json({'message': e}); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
module.exports = { | ||
getAllSubscriptions, | ||
getUserSubscriptions, | ||
getSubscription, | ||
createNewSubscription, | ||
editSubscription, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const subscriptionSchema = new Schema({ | ||
type: { | ||
type: String, | ||
required: false | ||
}, | ||
status: { | ||
type: String, | ||
required: true | ||
}, | ||
expiresAt:{ | ||
type: Number, | ||
required: false | ||
}, | ||
stripeId: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
invoiceId:{ | ||
type: String, | ||
required: true, | ||
}, | ||
user: { | ||
type: Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true | ||
} | ||
}); | ||
|
||
subscriptionSchema.pre('remove', function(next) { | ||
console.log('remove subscription from users after deletion') | ||
let index = this.user.payments.indexOf(this._id); | ||
if (index > -1) { | ||
this.user.subscriptions.splice(index, 1); | ||
this.user.save(); | ||
} | ||
next(); | ||
}); | ||
|
||
module.exports = mongoose.model('Subscription', subscriptionSchema); |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const subscriptionsController = require('../../controllers/subscriptionsController'); | ||
const ROLES_LIST = require('../../config/roles_list'); | ||
const verifyRoles = require('../../middleware/verifyRoles'); | ||
|
||
router.route('/') | ||
.get(verifyRoles(ROLES_LIST.User, ROLES_LIST.Editor), subscriptionsController.getAllSubscriptions) | ||
|
||
// .put(verifyRoles(ROLES_LIST.User, ROLES_LIST.Editor), paymentsController.updatePayment) | ||
// .delete(verifyRoles(ROLES_LIST.User), paymentsController.deletePayment); | ||
|
||
router.route('/:id') | ||
.get(verifyRoles(ROLES_LIST.User, ROLES_LIST.Editor),subscriptionsController.getSubscription); | ||
|
||
router.route('/:id') | ||
.put(verifyRoles(ROLES_LIST.User), subscriptionsController.editSubscription); | ||
|
||
router.route('/user/:id') | ||
//.get(verifyRoles(ROLES_LIST.User, ROLES_LIST.Editor),paymentsController.getUserPayments) | ||
.post(verifyRoles(ROLES_LIST.User, ROLES_LIST.Editor), subscriptionsController.createNewSubscription); | ||
|
||
module.exports = router; |
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