sweet! can now update transactions

This commit is contained in:
Annika
2022-07-12 11:48:40 -06:00
parent 6455c02d5e
commit ef06dd057a
6 changed files with 78 additions and 16 deletions
+17 -14
View File
@@ -19,35 +19,38 @@
<input type="text" v-model="transaction.comment" :placeholder="transaction.comment" class="text-slate-50 bg-stone-800 w-48 hover:bg-green-300 hover:text-stone-900"> <input type="text" v-model="transaction.comment" :placeholder="transaction.comment" class="text-slate-50 bg-stone-800 w-48 hover:bg-green-300 hover:text-stone-900">
</div> </div>
<div> <div>
<button class="text-slate-50 bg-stone-800 w-16 hover:bg-green-300 hover:text-stone-900"> <button class="text-slate-50 bg-stone-800 w-16 hover:bg-green-300 hover:text-stone-900" @click="update">
Save Save
</button> </button>
{{transaction}}
</div> </div>
</div> </div>
</template> </template>
<script> <script>
// import { logger } from '../utils/Logger'
import { transactionsService } from '../services/TransactionsService'
export default { export default {
// TODO there's probably something wrong with this... // TODO there's probably something wrong with this...
props: { props: {
transaction: { transaction: {
date: String || '2022-07-11', type: Object,
amount: Number || 0, required: true
payee: String || '',
category: String || '', //should be enum
comment: String || ''
} }
}, },
setup(){ setup(props){
return { return {
// transaction: { async update() {
// date: '2022-07-11', console.log(props.transaction)
// amount: 0, // send off request to update transaction
// payee: '', try {
// category: '', await transactionsService.update(props.transaction)
// comment: '' } catch (error) {
// } console.log(error)
}
}
} }
} }
} }
+1
View File
@@ -16,6 +16,7 @@ const routes = [
name: 'Transactions', name: 'Transactions',
component: loadPage('TransactionsPage'), component: loadPage('TransactionsPage'),
beforeEnter: authGuard // this is important to keep the request from firing before the auth token is issued. beforeEnter: authGuard // this is important to keep the request from firing before the auth token is issued.
// if not need to be logged-in use authSettled
}, },
{ {
path: '/account', path: '/account',
+1 -1
View File
@@ -26,7 +26,7 @@ AuthService.on(AuthService.AUTH_EVENTS.AUTHENTICATED, async function() {
AppState.user = AuthService.user AppState.user = AuthService.user
await accountService.getAccount() await accountService.getAccount()
socketService.authenticate(AuthService.bearer) socketService.authenticate(AuthService.bearer)
// NOTE if there is something you want to do once the user is authenticated, place that here // NOTE if there is something you want to do once the user is authenticated, place that here (!!!)
}) })
async function refreshAuthToken(config) { async function refreshAuthToken(config) {
@@ -17,6 +17,14 @@ class TransactionsService {
// logger.log('transactions: ' + res.data) // logger.log('transactions: ' + res.data)
AppState.transactions = res.data AppState.transactions = res.data
} }
// TODO test this
async update(transaction) {
console.log(transaction._id)
const res = await api.put('api/transactions/' + transaction._id, transaction)
console.log('transactionUpdate: ' + res.data)
// AppState.transactions.find((t) => t._id == transactionId) = res.data
}
} }
export const transactionsService = new TransactionsService() export const transactionsService = new TransactionsService()
@@ -3,6 +3,7 @@ import { accountService } from '../services/AccountService'
import { categoriesService } from '../services/CategoriesService' import { categoriesService } from '../services/CategoriesService'
import { transactionsService } from '../services/TransactionsService' import { transactionsService } from '../services/TransactionsService'
import BaseController from '../utils/BaseController' import BaseController from '../utils/BaseController'
import { logger } from '../utils/Logger'
// export class CategoriesController extends BaseController { // export class CategoriesController extends BaseController {
// constructor() { // constructor() {
@@ -23,6 +24,7 @@ export class TransactionsController extends BaseController {
.use(Auth0Provider.getAuthorizedUserInfo) .use(Auth0Provider.getAuthorizedUserInfo)
.post('', this.create) .post('', this.create)
.get('', this.getAll) .get('', this.getAll)
.put('/:id', this.update)
} }
async create(req, res, next) { async create(req, res, next) {
@@ -43,4 +45,18 @@ export class TransactionsController extends BaseController {
next(error) next(error)
} }
} }
// TODO test this
async update(req, res, next) {
try {
// I'm tired of losing the people I love...
logger.log(req)
const updatedTransaction = await transactionsService.update(req.userInfo.id, req.params.id, req.body)
// I'm tired of not being enough...
return res.send(updatedTransaction)
} catch (error) {
// I'm tired of life...
next(error)
}
}
} }
@@ -1,5 +1,6 @@
import { dbContext } from '../db/DbContext' import { dbContext } from '../db/DbContext'
import { BadRequest } from '../utils/Errors' import { BadRequest } from '../utils/Errors'
import { logger } from '../utils/Logger'
class TransactionsService { class TransactionsService {
async create(newTransaction) { async create(newTransaction) {
@@ -18,7 +19,40 @@ async getAll(accountId) {
return allTransactions return allTransactions
} }
// TODO test this
// TODO fix this
async update(accountId, transactionId, update) {
if (!transactionId) {
throw new BadRequest('Bad transaction id')
}
let original = await dbContext.Transactions.findById(transactionId)
if (accountId != original.accountId) {
throw new BadRequest('Das nacho transaction')
}
logger.log('original: ' + original)
logger.log('update: ' + update)
original.categoryId = update.categoryId || original.categoryId
original.amount = update.amount || original.amount
original.date = update.date || original.date
original.payee = update.payee || original.payee
original.comment = update.comment || original.comment
update = await dbContext.Transactions.findByIdAndUpdate(transactionId, original)
return update
}
} }
export const transactionsService = new TransactionsService() export const transactionsService = new TransactionsService()
// async update(id, update) {
// const original = await dbContext.Projects.findById(id).populate('creator', 'name picture')
// if (original.creatorId.toString() != update.creatorId) {
// throw new BadRequest("you don't have permission to edit that project")
// }
// original.name = update.name ? update.name : original.name
// original.description = update.description ? update.description : original.description
// original.category = update.category ? update.category : original.category
// original.save()
// return update
// }