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">
</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
</button>
{{transaction}}
</div>
</div>
</template>
<script>
// import { logger } from '../utils/Logger'
import { transactionsService } from '../services/TransactionsService'
export default {
// TODO there's probably something wrong with this...
props: {
transaction: {
date: String || '2022-07-11',
amount: Number || 0,
payee: String || '',
category: String || '', //should be enum
comment: String || ''
type: Object,
required: true
}
},
setup(){
setup(props){
return {
// transaction: {
// date: '2022-07-11',
// amount: 0,
// payee: '',
// category: '',
// comment: ''
// }
async update() {
console.log(props.transaction)
// send off request to update transaction
try {
await transactionsService.update(props.transaction)
} catch (error) {
console.log(error)
}
}
}
}
}
+1
View File
@@ -16,6 +16,7 @@ const routes = [
name: 'Transactions',
component: loadPage('TransactionsPage'),
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',
+1 -1
View File
@@ -26,7 +26,7 @@ AuthService.on(AuthService.AUTH_EVENTS.AUTHENTICATED, async function() {
AppState.user = AuthService.user
await accountService.getAccount()
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) {
@@ -17,6 +17,14 @@ class TransactionsService {
// logger.log('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()
@@ -3,6 +3,7 @@ import { accountService } from '../services/AccountService'
import { categoriesService } from '../services/CategoriesService'
import { transactionsService } from '../services/TransactionsService'
import BaseController from '../utils/BaseController'
import { logger } from '../utils/Logger'
// export class CategoriesController extends BaseController {
// constructor() {
@@ -23,6 +24,7 @@ export class TransactionsController extends BaseController {
.use(Auth0Provider.getAuthorizedUserInfo)
.post('', this.create)
.get('', this.getAll)
.put('/:id', this.update)
}
async create(req, res, next) {
@@ -43,4 +45,18 @@ export class TransactionsController extends BaseController {
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 { BadRequest } from '../utils/Errors'
import { logger } from '../utils/Logger'
class TransactionsService {
async create(newTransaction) {
@@ -18,7 +19,40 @@ async getAll(accountId) {
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()
// 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
// }