support storing geo location in transaction

This commit is contained in:
MaysWind
2023-04-28 21:22:12 +08:00
parent 1ac968f63c
commit e71ffd1a77
9 changed files with 207 additions and 30 deletions
+20 -4
View File
@@ -1,6 +1,7 @@
package api
import (
"encoding/json"
"sort"
"strings"
@@ -572,7 +573,7 @@ func (a *TransactionsApi) TransactionGetHandler(c *core.Context) (interface{}, *
transactionEditable := transaction.IsEditable(user, utcOffset, accountMap[transaction.AccountId], accountMap[transaction.RelatedAccountId])
transactionTagIds := allTransactionTagIds[transaction.TransactionId]
transactionResp := transaction.ToTransactionInfoResponse(transactionTagIds, transactionEditable)
transactionResp := transaction.ToTransactionInfoResponse(c, transactionTagIds, transactionEditable)
if !transactionGetReq.TrimAccount {
if sourceAccount := accountMap[transaction.AccountId]; sourceAccount != nil {
@@ -664,7 +665,7 @@ func (a *TransactionsApi) TransactionCreateHandler(c *core.Context) (interface{}
log.InfofWithRequestId(c, "[transactions.TransactionCreateHandler] user \"uid:%d\" has created a new transaction \"id:%d\" successfully", uid, transaction.TransactionId)
transactionResp := transaction.ToTransactionInfoResponse(tagIds, transactionEditable)
transactionResp := transaction.ToTransactionInfoResponse(c, tagIds, transactionEditable)
return transactionResp, nil
}
@@ -722,6 +723,12 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
transactionTagIds = make([]int64, 0, 0)
}
var geoLocation []byte
if transactionModifyReq.GeoLocation != nil {
geoLocation, _ = json.Marshal(transactionModifyReq.GeoLocation)
}
newTransaction := &models.Transaction{
TransactionId: transaction.TransactionId,
Uid: uid,
@@ -732,6 +739,7 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
Amount: transactionModifyReq.SourceAmount,
HideAmount: transactionModifyReq.HideAmount,
Comment: transactionModifyReq.Comment,
GeoLocation: string(geoLocation),
}
if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_OUT {
@@ -748,6 +756,7 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
(transaction.Type != models.TRANSACTION_DB_TYPE_TRANSFER_OUT || newTransaction.RelatedAccountAmount == transaction.RelatedAccountAmount) &&
newTransaction.HideAmount == transaction.HideAmount &&
newTransaction.Comment == transaction.Comment &&
newTransaction.GeoLocation == transaction.GeoLocation &&
utils.Int64SliceEquals(tagIds, transactionTagIds) {
return nil, errs.ErrNothingWillBeUpdated
}
@@ -777,7 +786,7 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
log.InfofWithRequestId(c, "[transactions.TransactionModifyHandler] user \"uid:%d\" has updated transaction \"id:%d\" successfully", uid, transactionModifyReq.Id)
newTransaction.Type = transaction.Type
newTransactionResp := newTransaction.ToTransactionInfoResponse(tagIds, transactionEditable)
newTransactionResp := newTransaction.ToTransactionInfoResponse(c, tagIds, transactionEditable)
return newTransactionResp, nil
}
@@ -1004,7 +1013,7 @@ func (a *TransactionsApi) getTransactionListResult(c *core.Context, user *models
transactionEditable := transaction.IsEditable(user, utcOffset, allAccounts[transaction.AccountId], allAccounts[transaction.RelatedAccountId])
transactionTagIds := allTransactionTagIds[transaction.TransactionId]
result[i] = transaction.ToTransactionInfoResponse(transactionTagIds, transactionEditable)
result[i] = transaction.ToTransactionInfoResponse(c, transactionTagIds, transactionEditable)
if !trimAccount {
if sourceAccount := allAccounts[transaction.AccountId]; sourceAccount != nil {
@@ -1045,6 +1054,12 @@ func (a *TransactionsApi) createNewTransactionModel(uid int64, transactionCreate
transactionDbType = models.TRANSACTION_DB_TYPE_TRANSFER_OUT
}
var geoLocation []byte
if transactionCreateReq.GeoLocation != nil {
geoLocation, _ = json.Marshal(transactionCreateReq.GeoLocation)
}
transaction := &models.Transaction{
Uid: uid,
Type: transactionDbType,
@@ -1055,6 +1070,7 @@ func (a *TransactionsApi) createNewTransactionModel(uid int64, transactionCreate
Amount: transactionCreateReq.SourceAmount,
HideAmount: transactionCreateReq.HideAmount,
Comment: transactionCreateReq.Comment,
GeoLocation: string(geoLocation),
CreatedIp: clientIp,
}
+54 -23
View File
@@ -1,11 +1,14 @@
package models
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)
@@ -48,40 +51,49 @@ type Transaction struct {
RelatedAccountAmount int64 `xorm:"NOT NULL"`
HideAmount bool `xorm:"NOT NULL"`
Comment string `xorm:"VARCHAR(255) NOT NULL"`
GeoLocation string `xorm:"VARCHAR(255)"`
CreatedIp string `xorm:"VARCHAR(39)"`
CreatedUnixTime int64
UpdatedUnixTime int64
DeletedUnixTime int64
}
// TransactionGeoLocationRequest represents all parameters of transaction geographic location info update request
type TransactionGeoLocationRequest struct {
Latitude float64 `json:"latitude" binding:"required"`
Longitude float64 `json:"longitude" binding:"required"`
}
// TransactionCreateRequest represents all parameters of transaction creation request
type TransactionCreateRequest struct {
Type TransactionType `json:"type" binding:"required"`
CategoryId int64 `json:"categoryId,string"`
Time int64 `json:"time" binding:"required,min=1"`
UtcOffset int16 `json:"utcOffset" binding:"min=-720,max=840"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"min=0"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
HideAmount bool `json:"hideAmount"`
TagIds []string `json:"tagIds"`
Comment string `json:"comment" binding:"max=255"`
Type TransactionType `json:"type" binding:"required"`
CategoryId int64 `json:"categoryId,string"`
Time int64 `json:"time" binding:"required,min=1"`
UtcOffset int16 `json:"utcOffset" binding:"min=-720,max=840"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"min=0"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
HideAmount bool `json:"hideAmount"`
TagIds []string `json:"tagIds"`
Comment string `json:"comment" binding:"max=255"`
GeoLocation *TransactionGeoLocationRequest `json:"geoLocation" binding:"omitempty"`
}
// TransactionModifyRequest represents all parameters of transaction modification request
type TransactionModifyRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
CategoryId int64 `json:"categoryId,string"`
Time int64 `json:"time" binding:"required,min=1"`
UtcOffset int16 `json:"utcOffset" binding:"min=-720,max=840"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"min=0"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
HideAmount bool `json:"hideAmount"`
TagIds []string `json:"tagIds"`
Comment string `json:"comment" binding:"max=255"`
Id int64 `json:"id,string" binding:"required,min=1"`
CategoryId int64 `json:"categoryId,string"`
Time int64 `json:"time" binding:"required,min=1"`
UtcOffset int16 `json:"utcOffset" binding:"min=-720,max=840"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"min=0"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
HideAmount bool `json:"hideAmount"`
TagIds []string `json:"tagIds"`
Comment string `json:"comment" binding:"max=255"`
GeoLocation *TransactionGeoLocationRequest `json:"geoLocation" binding:"omitempty"`
}
// TransactionCountRequest represents transaction count request
@@ -170,6 +182,12 @@ type TransactionAccountAmount struct {
TotalExpenseAmount int64
}
// TransactionGeoLocationResponse represents a view-object of transaction geographic location info
type TransactionGeoLocationResponse struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
// TransactionInfoResponse represents a view-object of transaction
type TransactionInfoResponse struct {
Id int64 `json:"id,string"`
@@ -189,6 +207,7 @@ type TransactionInfoResponse struct {
TagIds []string `json:"tagIds"`
Tags []*TransactionTagInfoResponse `json:"tags,omitempty"`
Comment string `json:"comment"`
GeoLocation *TransactionGeoLocationResponse `json:"geoLocation,omitempty"`
Editable bool `json:"editable"`
}
@@ -264,7 +283,7 @@ func (t *Transaction) IsEditable(currentUser *User, utcOffset int16, account *Ac
}
// ToTransactionInfoResponse returns a view-object according to database model
func (t *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *TransactionInfoResponse {
func (t *Transaction) ToTransactionInfoResponse(c *core.Context, tagIds []int64, editable bool) *TransactionInfoResponse {
var transactionType TransactionType
if t.Type == TRANSACTION_DB_TYPE_MODIFY_BALANCE {
@@ -298,6 +317,17 @@ func (t *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *
destinationAmount = t.Amount
}
geoLocation := &TransactionGeoLocationResponse{}
if t.GeoLocation != "" {
err := json.Unmarshal([]byte(t.GeoLocation), geoLocation)
if err != nil {
log.WarnfWithRequestId(c, "[transaction.ToTransactionInfoResponse] cannot unmarshal geo location \"%s\", because %s", t.GeoLocation, err.Error())
}
} else {
geoLocation = nil
}
return &TransactionInfoResponse{
Id: t.TransactionId,
TimeSequenceId: t.TransactionTime,
@@ -312,6 +342,7 @@ func (t *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *
HideAmount: t.HideAmount,
TagIds: utils.Int64ArrayToStringArray(tagIds),
Comment: t.Comment,
GeoLocation: geoLocation,
Editable: editable,
}
}
+5
View File
@@ -542,6 +542,10 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction,
updateCols = append(updateCols, "comment")
}
if transaction.GeoLocation != oldTransaction.GeoLocation {
updateCols = append(updateCols, "geo_location")
}
// Get and verify tags
err = s.isTagsValid(sess, transaction, transactionTagIndexs, addTagIds)
@@ -955,6 +959,7 @@ func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *
RelatedAccountId: originalTransaction.AccountId,
RelatedAccountAmount: originalTransaction.Amount,
Comment: originalTransaction.Comment,
GeoLocation: originalTransaction.GeoLocation,
CreatedIp: originalTransaction.CreatedIp,
CreatedUnixTime: originalTransaction.CreatedUnixTime,
UpdatedUnixTime: originalTransaction.UpdatedUnixTime,
+4 -2
View File
@@ -266,7 +266,7 @@ export default {
getTransaction: ({ id }) => {
return axios.get(`v1/transactions/get.json?id=${id}&trim_account=true&trim_category=true&trim_tag=true`);
},
addTransaction: ({ type, categoryId, time, sourceAccountId, destinationAccountId, sourceAmount, destinationAmount, hideAmount, tagIds, comment, utcOffset }) => {
addTransaction: ({ type, categoryId, time, sourceAccountId, destinationAccountId, sourceAmount, destinationAmount, hideAmount, tagIds, comment, geoLocation, utcOffset }) => {
return axios.post('v1/transactions/add.json', {
type,
categoryId,
@@ -278,10 +278,11 @@ export default {
hideAmount,
tagIds,
comment,
geoLocation,
utcOffset
});
},
modifyTransaction: ({ id, type, categoryId, time, sourceAccountId, destinationAccountId, sourceAmount, destinationAmount, hideAmount, tagIds, comment, utcOffset }) => {
modifyTransaction: ({ id, type, categoryId, time, sourceAccountId, destinationAccountId, sourceAmount, destinationAmount, hideAmount, tagIds, comment, geoLocation, utcOffset }) => {
return axios.post('v1/transactions/modify.json', {
id,
type,
@@ -294,6 +295,7 @@ export default {
hideAmount,
tagIds,
comment,
geoLocation,
utcOffset
});
},
+3
View File
@@ -13,6 +13,7 @@ const defaultSettings = {
applicationLock: false,
applicationLockWebAuthn: false,
autoUpdateExchangeRatesData: true,
autoGetCurrentGeoLocation: false,
thousandsSeparator: true,
currencyDisplayMode: currencyConstants.defaultCurrencyDisplayMode,
showAmountInHomePage: true,
@@ -140,6 +141,8 @@ export default {
setEnableApplicationLockWebAuthn: value => setOption('applicationLockWebAuthn', value),
isAutoUpdateExchangeRatesData: () => getOption('autoUpdateExchangeRatesData'),
setAutoUpdateExchangeRatesData: value => setOption('autoUpdateExchangeRatesData', value),
isAutoGetCurrentGeoLocation: () => getOption('autoGetCurrentGeoLocation'),
setAutoGetCurrentGeoLocation: value => setOption('autoGetCurrentGeoLocation', value),
isEnableThousandsSeparator: () => getOption('thousandsSeparator'),
setEnableThousandsSeparator: value => setOption('thousandsSeparator', value),
getCurrencyDisplayMode: () => getOption('currencyDisplayMode'),
+7
View File
@@ -830,6 +830,12 @@ export default {
'Destination Account': 'Destination Account',
'Transaction Time': 'Transaction Time',
'Transaction Time Zone': 'Transaction Time Zone',
'Geographic Location': 'Geographic Location',
'No Location': 'No Location',
'Getting Location...': 'Getting Location...',
'Update Geographic Location': 'Update Geographic Location',
'Clear Geographic Location': 'Clear Geographic Location',
'Unable to get current position': 'Unable to get current position',
'Tags': 'Tags',
'Your transaction description (optional)': 'Your transaction description (optional)',
'Are you sure you want to save this transaction whose amount is 0?': 'Are you sure you want to save this transaction whose amount is 0?',
@@ -881,6 +887,7 @@ export default {
'Timezone': 'Timezone',
'System Default': 'System Default',
'Auto Update Exchange Rates Data': 'Auto Update Exchange Rates Data',
'Auto Get Current Geographic Location': 'Auto Get Current Geographic Location',
'Enable Thousands Separator': 'Enable Thousands Separator',
'Currency Display Mode': 'Currency Display Mode',
'Currency Code': 'Currency Code',
+7
View File
@@ -830,6 +830,12 @@ export default {
'Destination Account': '目标账户',
'Transaction Time': '交易时间',
'Transaction Time Zone': '交易时区',
'Geographic Location': '地理位置',
'No Location': '没有位置',
'Getting Location...': '正在获取位置...',
'Update Geographic Location': '更新地理位置',
'Clear Geographic Location': '清除地理位置',
'Unable to get current position': '无法获取当前地理位置',
'Tags': '标签',
'Your transaction description (optional)': '你的交易描述 (可选)',
'Are you sure you want to save this transaction whose amount is 0?': '您确定要保存这个金额为0的交易?',
@@ -881,6 +887,7 @@ export default {
'Timezone': '时区',
'System Default': '系统默认',
'Auto Update Exchange Rates Data': '自动更新汇率数据',
'Auto Get Current Geographic Location': '自动获取当前地理位置',
'Enable Thousands Separator': '启用千位分隔符',
'Currency Display Mode': '货币显示模式',
'Currency Code': '货币代码',
+13
View File
@@ -46,6 +46,11 @@
<f7-toggle :checked="isAutoUpdateExchangeRatesData" @toggle:change="isAutoUpdateExchangeRatesData = $event"></f7-toggle>
</f7-list-item>
<f7-list-item>
<span>{{ $t('Auto Get Current Geographic Location') }}</span>
<f7-toggle :checked="isAutoGetCurrentGeoLocation" @toggle:change="isAutoGetCurrentGeoLocation = $event"></f7-toggle>
</f7-list-item>
<f7-list-item>
<span>{{ $t('Enable Thousands Separator') }}</span>
<f7-toggle :checked="isEnableThousandsSeparator" @toggle:change="isEnableThousandsSeparator = $event"></f7-toggle>
@@ -149,6 +154,14 @@ export default {
this.$settings.setAutoUpdateExchangeRatesData(value);
}
},
isAutoGetCurrentGeoLocation: {
get: function () {
return this.$settings.isAutoGetCurrentGeoLocation();
},
set: function (value) {
this.$settings.setAutoGetCurrentGeoLocation(value);
}
},
isEnableThousandsSeparator: {
get: function () {
return this.$settings.isEnableThousandsSeparator();
+94 -1
View File
@@ -27,6 +27,7 @@
<f7-list-item class="list-item-with-header-and-title" header="Account" title="Account Name"></f7-list-item>
<f7-list-input label="Transaction Time" placeholder="YYYY/MM/DD HH:mm"></f7-list-input>
<f7-list-item :no-chevron="mode === 'view'" class="list-item-with-header-and-title list-item-title-hide-overflow" header="Transaction Time Zone" title="(UTC XX:XX) System Default" link="#"></f7-list-item>
<f7-list-item class="list-item-with-header-and-title list-item-title-hide-overflow" header="Geographic Location" title="No Location" link="#"></f7-list-item>
<f7-list-item header="Tags">
<template #footer>
<f7-block class="margin-top-half no-padding no-margin">
@@ -236,6 +237,20 @@
</template>
</f7-list-item>
<f7-list-item
link="#" no-chevron
class="list-item-with-header-and-title list-item-title-hide-overflow"
:header="$t('Geographic Location')"
@click="showGeoLocationActionSheet = true"
>
<template #title>
<f7-block class="list-item-custom-title no-padding no-margin">
<span v-if="transaction.geoLocation">{{ `(${transaction.geoLocation.longitude}, ${transaction.geoLocation.latitude})` }}</span>
<span v-else-if="!transaction.geoLocation">{{ geoLocationStatusInfo }}</span>
</f7-block>
</template>
</f7-list-item>
<f7-list-item
link="#" no-chevron
:header="$t('Tags')"
@@ -275,6 +290,16 @@
></f7-list-input>
</f7-list>
<f7-actions close-by-outside-click close-on-escape :opened="showGeoLocationActionSheet" @actions:closed="showGeoLocationActionSheet = false">
<f7-actions-group>
<f7-actions-button v-if="mode !== 'view'" @click="updateGeoLocation(true)">{{ $t('Update Geographic Location') }}</f7-actions-button>
<f7-actions-button v-if="mode !== 'view'" @click="clearGeoLocation">{{ $t('Clear Geographic Location') }}</f7-actions-button>
</f7-actions-group>
<f7-actions-group>
<f7-actions-button bold close>{{ $t('Cancel') }}</f7-actions-button>
</f7-actions-group>
</f7-actions>
<f7-actions close-by-outside-click close-on-escape :opened="showMoreActionSheet" @actions:closed="showMoreActionSheet = false">
<f7-actions-group>
<f7-actions-button v-if="transaction.hideAmount" @click="transaction.hideAmount = false">{{ $t('Show Amount') }}</f7-actions-button>
@@ -330,12 +355,16 @@ export default {
destinationAmount: 0,
hideAmount: false,
tagIds: [],
comment: ''
comment: '',
geoLocation: null
},
loading: true,
loadingError: null,
geoLocationStatus: null,
submitting: false,
isSupportGeoLocation: !!navigator.geolocation,
showAccountBalance: self.$settings.isShowAccountBalance(),
showGeoLocationActionSheet: false,
showMoreActionSheet: false,
showSourceAmountSheet: false,
showDestinationAmountSheet: false,
@@ -507,6 +536,15 @@ export default {
destinationAmountFontSize() {
return this.getFontSizeByAmount(this.transaction.destinationAmount);
},
geoLocationStatusInfo() {
if (this.geoLocationStatus === 'success') {
return '';
} else if (this.geoLocationStatus === 'getting') {
return this.$t('Getting Location...');
} else {
return this.$t('No Location');
}
},
inputIsEmpty() {
return !!this.inputEmptyProblemMessage;
},
@@ -711,6 +749,10 @@ export default {
self.transaction.hideAmount = transaction.hideAmount;
self.transaction.tagIds = transaction.tagIds || [];
self.transaction.comment = transaction.comment;
if (self.mode === 'edit' || self.mode === 'view') {
self.transaction.geoLocation = transaction.geoLocation;
}
}
self.loading = false;
@@ -728,6 +770,11 @@ export default {
methods: {
onPageAfterIn() {
this.$routeBackOnError(this.f7router, 'loadingError');
if (this.$settings.isAutoGetCurrentGeoLocation() && this.mode === 'add'
&& !this.geoLocationStatus && !this.transaction.geoLocation) {
this.updateGeoLocation(false);
}
},
save() {
const self = this;
@@ -747,6 +794,7 @@ export default {
hideAmount: self.transaction.hideAmount,
tagIds: self.transaction.tagIds,
comment: self.transaction.comment,
geoLocation: self.transaction.geoLocation,
utcOffset: self.transaction.utcOffset
};
@@ -803,6 +851,51 @@ export default {
doSubmit();
}
},
updateGeoLocation(forceUpdate) {
const self = this;
if (!self.isSupportGeoLocation) {
self.$logger.warn('this browser does not support geo location');
if (forceUpdate) {
self.$toast('Unable to get current position');
}
return;
}
navigator.geolocation.getCurrentPosition(function (position) {
if (!position || !position.coords) {
self.$logger.error('current position is null');
self.geoLocationStatus = 'error';
if (forceUpdate) {
self.$toast('Unable to get current position');
}
return;
}
self.geoLocationStatus = 'success';
self.transaction.geoLocation = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
}, function (err) {
self.$logger.error('cannot get current position', err);
self.geoLocationStatus = 'error';
if (forceUpdate) {
self.$toast('Unable to get current position');
}
});
self.geoLocationStatus = 'getting';
},
clearGeoLocation() {
this.geoLocationStatus = null;
this.transaction.geoLocation = null;
},
isCategoryIdAvailable(categories, categoryId) {
if (!categories || !categories.length) {
return false;