Merge pull request #14 from pnagurny/feature/budget

Feature/budget
This commit is contained in:
Patrick Nagurny
2020-01-14 14:32:18 -05:00
committed by GitHub
24 changed files with 705 additions and 246 deletions

View File

@@ -14,12 +14,12 @@ import (
/**
* @api {get} /orgs/:orgId/accounts Get Accounts by Org id
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetOrgAccounts
* @apiGroup Account
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Account.
* @apiSuccess {String} orgId Id of the Org.
@@ -86,12 +86,12 @@ func GetOrgAccounts(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {get} /orgs/:orgId/accounts/:accountId Get Acount by Org id and Account id
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetOrgAccount
* @apiGroup Account
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Account.
* @apiSuccess {String} orgId Id of the Org.
@@ -157,12 +157,12 @@ func GetOrgAccount(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /orgs/:orgId/accounts Create a new Account
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostAccount
* @apiGroup Account
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id Id 32 character hex string
* @apiParam {String} name Name of the Account.
@@ -270,12 +270,12 @@ func PostAccounts(w rest.ResponseWriter, r *rest.Request, content []byte) {
/**
* @api {put} /orgs/:orgId/accounts/:accountId Modify an Account
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutAccount
* @apiGroup Account
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id Id 32 character hex string
* @apiParam {String} name Name of the Account.
@@ -345,12 +345,12 @@ func PutAccount(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /orgs/:orgId/accounts/:accountId Delete an Account
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeleteAccount
* @apiGroup Account
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

View File

@@ -7,7 +7,12 @@ import (
/**
* Changelog
*
* 1.3.0
* 1.4.0
* - add `GET /orgs/:orgId/budget`
* - add `POST /orgs/:orgId/budget`
* - add `DELETE /orgs/:orgId/budget`
*
* 1.4.0
* - add org.timezone
*
* 1.2.0

View File

@@ -9,12 +9,12 @@ import (
/**
* @api {get} /apikeys Get API keys
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetApiKeys
* @apiGroup ApiKey
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the ApiKey.
* @apiSuccess {Date} inserted Date ApiKey was created
@@ -52,11 +52,11 @@ func GetApiKeys(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /apikeys Create a new API key
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostApiKey
* @apiGroup ApiKey
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
* @apiHeader {String} Authorization HTTP Basic Auth
*
* @apiParam {String} id 32 character hex string
@@ -105,11 +105,11 @@ func PostApiKey(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {put} /apikeys Modify an API key
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutApiKey
* @apiGroup ApiKey
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
* @apiHeader {String} Authorization HTTP Basic Auth
*
* @apiParam {String} id 32 character hex string
@@ -160,12 +160,12 @@ func PutApiKey(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /apikeys/:apiKeyId Delete an API key
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeleteApiKey
* @apiGroup ApiKey
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

149
core/api/budget.go Normal file
View File

@@ -0,0 +1,149 @@
package api
import (
"github.com/ant0ine/go-json-rest/rest"
"github.com/openaccounting/oa-server/core/model"
"github.com/openaccounting/oa-server/core/model/types"
"net/http"
)
/**
* @api {get} /orgs/:orgId/budget Get Budget
* @apiVersion 1.4.0
* @apiName GetBudget
* @apiGroup Budget
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} orgId Id of the Org.
* @apiSuccess {Date} inserted Date Transaction was created
* @apiSuccess {Object[]} items Array of Budget Items
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "orgId": "11111111111111111111111111111111",
* "inserted": "2020-01-13T20:12:29.720Z",
* "items": [
* {
* "accountId": "11111111111111111111111111111111",
* "amount": 35000,
* },
* {
* "accountId": "22222222222222222222222222222222",
* "amount": 55000
* }
* ]
* }
* ]
*
* @apiUse NotAuthorizedError
* @apiUse InternalServerError
*/
func GetBudget(w rest.ResponseWriter, r *rest.Request) {
user := r.Env["USER"].(*types.User)
orgId := r.PathParam("orgId")
budget, err := model.Instance.GetBudget(orgId, user.Id)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteJson(&budget)
}
/**
* @api {post} /orgs/:orgId/budget Create a Budget
* @apiVersion 1.4.0
* @apiName PostBudget
* @apiGroup Budget
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {Object[]} items Array of Budget Items.
* @apiParam {String} items.accountId Id of Expense Account
* @apiParam {Number} items.amount Amount budgeted
*
* @apiSuccess {String} orgId Id of the Org.
* @apiSuccess {Date} inserted Date Transaction was created
* @apiSuccess {Object[]} items Array of Budget Items
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "orgId": "11111111111111111111111111111111",
* "inserted": "2020-01-13T20:12:29.720Z",
* "items": [
* {
* "accountId": "11111111111111111111111111111111",
* "amount": 35000,
* },
* {
* "accountId": "22222222222222222222222222222222",
* "amount": 55000
* }
* ]
* }
* ]
*
* @apiUse NotAuthorizedError
* @apiUse InternalServerError
*/
func PostBudget(w rest.ResponseWriter, r *rest.Request) {
user := r.Env["USER"].(*types.User)
orgId := r.PathParam("orgId")
budget := types.Budget{}
err := r.DecodeJsonPayload(&budget)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
budget.OrgId = orgId
err = model.Instance.CreateBudget(&budget, user.Id)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteJson(budget)
}
/**
* @api {delete} /orgs/:orgId/budget Delete Budget
* @apiVersion 1.4.0
* @apiName DeleteBudget
* @apiGroup Budget
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
*
* @apiUse NotAuthorizedError
* @apiUse InternalServerError
*/
func DeleteBudget(w rest.ResponseWriter, r *rest.Request) {
user := r.Env["USER"].(*types.User)
orgId := r.PathParam("orgId")
err := model.Instance.DeleteBudget(orgId, user.Id)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}

View File

@@ -9,12 +9,12 @@ import (
/**
* @api {get} /org/:orgId Get Org by id
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetOrg
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Org.
* @apiSuccess {Date} inserted Date Org was created
@@ -54,39 +54,39 @@ func GetOrg(w rest.ResponseWriter, r *rest.Request) {
}
/**
* @api {get} /orgs Get a User's Orgs
* @apiVersion 1.3.0
* @apiName GetOrgs
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
*
* @apiSuccess {String} id Id of the Org.
* @apiSuccess {Date} inserted Date Org was created
* @apiSuccess {Date} updated Date Org was updated
* @apiSuccess {String} name Name of the Org.
* @apiSuccess {String} currency Three letter currency code.
* @apiSuccess {Number} precision How many digits the currency goes out to.
@apiSuccess {String} timezone Timezone to use for accounting.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "id": "11111111111111111111111111111111",
* "inserted": "2018-09-11T18:05:04.420Z",
* "updated": "2018-09-11T18:05:04.420Z",
* "name": "MyOrg",
* "currency": "USD",
* "precision": 2,
* "timezone": "America/New_York"
* }
* ]
*
* @apiUse NotAuthorizedError
* @apiUse InternalServerError
*/
* @api {get} /orgs Get a User's Orgs
* @apiVersion 1.4.0
* @apiName GetOrgs
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Org.
* @apiSuccess {Date} inserted Date Org was created
* @apiSuccess {Date} updated Date Org was updated
* @apiSuccess {String} name Name of the Org.
* @apiSuccess {String} currency Three letter currency code.
* @apiSuccess {Number} precision How many digits the currency goes out to.
@apiSuccess {String} timezone Timezone to use for accounting.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "id": "11111111111111111111111111111111",
* "inserted": "2018-09-11T18:05:04.420Z",
* "updated": "2018-09-11T18:05:04.420Z",
* "name": "MyOrg",
* "currency": "USD",
* "precision": 2,
* "timezone": "America/New_York"
* }
* ]
*
* @apiUse NotAuthorizedError
* @apiUse InternalServerError
*/
func GetOrgs(w rest.ResponseWriter, r *rest.Request) {
user := r.Env["USER"].(*types.User)
@@ -102,12 +102,12 @@ func GetOrgs(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /orgs Create a new Org
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostOrg
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id Id 32 character hex string
* @apiParam {String} name Name of the Org.
@@ -159,12 +159,12 @@ func PostOrg(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {put} /orgs/:orgId Modify an Org
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutOrg
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} name Name of the Org.
*
@@ -216,12 +216,12 @@ func PutOrg(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /orgs/:orgId/invites Invite a user to an Org
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostInvite
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} email Email address of user
*
@@ -272,12 +272,12 @@ func PostInvite(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {put} /orgs/:orgId/invites/:inviteId Accept an invitation
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutInvite
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} accepted true
*
@@ -329,12 +329,12 @@ func PutInvite(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {get} /orgs/:orgId/invites Get Org invites
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetInvites
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Invite
* @apiSuccess {orgId} id Id of the Org
@@ -375,12 +375,12 @@ func GetInvites(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /orgs/:orgId/invites/:inviteId Delete Invite
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeleteInvite
* @apiGroup Org
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

View File

@@ -11,12 +11,12 @@ import (
/**
* @api {get} /org/:orgId/prices Get prices nearest in time or by currency
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetPrices
* @apiGroup Price
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {Number} nearestDate Milliseconds since epoch
* @apiParam {String} currency Currency code
@@ -93,12 +93,12 @@ func GetPrices(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /orgs/:orgId/prices Create a new Price
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostPrice
* @apiGroup Price
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id Id 32 character hex string.
* @apiParam {String} orgId Id of the Org.
@@ -155,12 +155,12 @@ func PostPrice(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /orgs/:orgId/prices/:priceId Delete a Price
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeletePrice
* @apiGroup Price
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

View File

@@ -46,5 +46,8 @@ func GetRouter(auth *AuthMiddleware, prefix string) (rest.App, error) {
rest.Put(prefix+"/orgs/:orgId/invites/:inviteId", auth.RequireAuth(PutInvite)),
rest.Delete(prefix+"/orgs/:orgId/invites/:inviteId", auth.RequireAuth(DeleteInvite)),
rest.Get(prefix+"/health-check", GetSystemHealthStatus),
rest.Get(prefix+"/orgs/:orgId/budget", auth.RequireAuth(GetBudget)),
rest.Post(prefix+"/orgs/:orgId/budget", auth.RequireAuth(PostBudget)),
rest.Delete(prefix+"/orgs/:orgId/budget", auth.RequireAuth(DeleteBudget)),
)
}

View File

@@ -9,11 +9,11 @@ import (
/**
* @api {post} /sessions Create a new Session
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostSession
* @apiGroup Session
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
* @apiHeader {String} Authorization HTTP Basic Auth
*
* @apiParam {String} id 32 character hex string
@@ -59,12 +59,12 @@ func PostSession(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /sessions/:sessionId Log out of a Session
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeleteSession
* @apiGroup Session
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

View File

@@ -7,12 +7,12 @@ import (
/**
* @api {get} /health-check Get system health status
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetSystemHealthStatus
* @apiGroup SystemHealth
*
*
* @apiHeader {String} Accept-Version: 1.3.0 semver versioning
* @apiHeader {String} Accept-Version: 1.4.0 semver versioning
*
* @apiSuccess {String} database Database status: "ok"; "fail"
* @apiSuccess {String} api API status: "ok"

View File

@@ -9,12 +9,12 @@ import (
/**
* @api {get} /orgs/:orgId/accounts/:accountId/transactions Get Transactions by Account Id
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetAccountTransactions
* @apiGroup Transaction
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Transaction.
* @apiSuccess {String} orgId Id of the Org.
@@ -85,12 +85,12 @@ func GetTransactionsByAccount(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {get} /orgs/:orgId/transactions Get Transactions by Org Id
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetOrgTransactions
* @apiGroup Transaction
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the Transaction.
* @apiSuccess {String} orgId Id of the Org.
@@ -160,12 +160,12 @@ func GetTransactionsByOrg(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /orgs/:orgId/transactions Create a new Transaction
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostTransaction
* @apiGroup Transaction
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id Id 32 character hex string
* @apiParam {Date} date Date of the Transaction
@@ -246,12 +246,12 @@ func PostTransaction(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {put} /orgs/:orgId/transactions/:transactionId Modify a Transaction
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutTransaction
* @apiGroup Transaction
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id 32 character hex string
* @apiParam {Date} date Date of the Transaction
@@ -333,12 +333,12 @@ func PutTransaction(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {delete} /orgs/:orgId/transactions/:transactionId Delete a Transaction
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName DeleteTransaction
* @apiGroup Transaction
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK

View File

@@ -22,12 +22,12 @@ type ResetPasswordParams struct {
/**
* @api {get} /user Get Authenticated User
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName GetUser
* @apiGroup User
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiSuccess {String} id Id of the User.
* @apiSuccess {Date} inserted Date User was created
@@ -64,11 +64,11 @@ func GetUser(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /users Create a new User
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PostUser
* @apiGroup User
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} id 32 character hex string
* @apiParam {String} firstName First name of the User.
@@ -123,12 +123,12 @@ func PostUser(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {put} /user Modify User
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName PutUser
* @apiGroup User
*
* @apiHeader {String} Authorization HTTP Basic Auth
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} password New password
* @apiParam {String} code Password reset code. (Instead of Authorization header)
@@ -204,11 +204,11 @@ func PutUser(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /user/verify Verify user email address
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName VerifyUser
* @apiGroup User
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} code Email verification code
*
@@ -238,11 +238,11 @@ func VerifyUser(w rest.ResponseWriter, r *rest.Request) {
/**
* @api {post} /user/reset-password Send reset password email
* @apiVersion 1.3.0
* @apiVersion 1.4.0
* @apiName ResetPassword
* @apiGroup User
*
* @apiHeader {String} Accept-Version ^1.3.0 semver versioning
* @apiHeader {String} Accept-Version ^1.4.0 semver versioning
*
* @apiParam {String} email Email address for user
*

View File

@@ -31,7 +31,7 @@ func (mw *VersionMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.Handl
rest.Error(writer, "Invalid version", http.StatusBadRequest)
}
serverVersion, _ := semver.NewVersion("1.3.0")
serverVersion, _ := semver.NewVersion("1.4.0")
// Pre-release versions
compatVersion, _ := semver.NewVersion("0.1.8")

58
core/model/budget.go Normal file
View File

@@ -0,0 +1,58 @@
package model
import (
"errors"
"github.com/openaccounting/oa-server/core/model/types"
)
type BudgetInterface interface {
GetBudget(string, string) (*types.Budget, error)
CreateBudget(*types.Budget, string) error
DeleteBudget(string, string) error
}
func (model *Model) GetBudget(orgId string, userId string) (*types.Budget, error) {
belongs, err := model.UserBelongsToOrg(userId, orgId)
if err != nil {
return nil, err
}
if belongs == false {
return nil, errors.New("User does not belong to org")
}
return model.db.GetBudget(orgId)
}
func (model *Model) CreateBudget(budget *types.Budget, userId string) error {
belongs, err := model.UserBelongsToOrg(userId, budget.OrgId)
if err != nil {
return err
}
if belongs == false {
return errors.New("User does not belong to org")
}
if budget.OrgId == "" {
return errors.New("orgId required")
}
return model.db.InsertAndReplaceBudget(budget)
}
func (model *Model) DeleteBudget(orgId string, userId string) error {
belongs, err := model.UserBelongsToOrg(userId, orgId)
if err != nil {
return err
}
if belongs == false {
return errors.New("User does not belong to org")
}
return model.db.DeleteBudget(orgId)
}

117
core/model/db/budget.go Normal file
View File

@@ -0,0 +1,117 @@
package db
import (
"errors"
"github.com/openaccounting/oa-server/core/model/types"
"github.com/openaccounting/oa-server/core/util"
"time"
)
type BudgetInterface interface {
GetBudget(string) (*types.Budget, error)
InsertAndReplaceBudget(*types.Budget) error
DeleteBudget(string) error
}
const budgetFields = "LOWER(HEX(accountId)),inserted,amount"
func (db *DB) GetBudget(orgId string) (*types.Budget, error) {
var budget types.Budget
var inserted int64
rows, err := db.Query("SELECT "+budgetFields+" FROM budgetitem WHERE orgId = UNHEX(?) ORDER BY HEX(accountId)", orgId)
if err != nil {
return nil, err
}
defer rows.Close()
items := make([]*types.BudgetItem, 0)
for rows.Next() {
i := new(types.BudgetItem)
err := rows.Scan(&i.AccountId, &inserted, &i.Amount)
if err != nil {
return nil, err
}
items = append(items, i)
}
err = rows.Err()
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, errors.New("Budget not found")
}
budget.OrgId = orgId
budget.Inserted = util.MsToTime(inserted)
budget.Items = items
return &budget, nil
}
func (db *DB) InsertAndReplaceBudget(budget *types.Budget) (err error) {
budget.Inserted = time.Now()
// Save to db
dbTx, err := db.Begin()
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
dbTx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
dbTx.Rollback()
} else {
err = dbTx.Commit()
}
}()
// delete previous budget
query1 := "DELETE FROM budgetitem WHERE orgId = UNHEX(?)"
_, err = dbTx.Exec(
query1,
budget.OrgId,
)
if err != nil {
return
}
// save items
for _, item := range budget.Items {
query := "INSERT INTO budgetitem(orgId,accountId,inserted,amount) VALUES (UNHEX(?),UNHEX(?),?,?)"
_, err = dbTx.Exec(
query,
budget.OrgId,
item.AccountId,
util.TimeToMs(budget.Inserted),
item.Amount)
if err != nil {
return
}
}
return
}
func (db *DB) DeleteBudget(orgId string) error {
query := "DELETE FROM budgetitem WHERE orgId = UNHEX(?)"
_, err := db.Exec(query, orgId)
return err
}

View File

@@ -19,6 +19,7 @@ type Datastore interface {
SessionInterface
ApiKeyInterface
SystemHealthInteface
BudgetInterface
}
func NewDB(dataSourceName string) (*DB, error) {

View File

@@ -23,6 +23,7 @@ type Interface interface {
SessionInterface
ApiKeyInterface
SystemHealthInteface
BudgetInterface
}
func NewModel(db db.Datastore, bcrypt util.Bcrypt, config types.Config) *Model {

View File

@@ -0,0 +1,17 @@
package types
import (
"time"
)
type Budget struct {
OrgId string `json:"orgId"`
Inserted time.Time `json:"inserted"`
Items []*BudgetItem `json:"items"`
}
type BudgetItem struct {
OrgId string `json:"-"`
AccountId string `json:"accountId"`
Amount int64 `json:"amount"`
}

View File

@@ -14,7 +14,7 @@ import (
"sync"
)
const version = "1.3.0"
const version = "1.4.0"
//var upgrader = websocket.Upgrader{} // use default options
var txSubscriptions = make(map[string][]*websocket.Conn)

View File

@@ -3,3 +3,4 @@ CREATE INDEX split_accountId_index ON split (accountId);
CREATE INDEX split_transactionId_index ON split (transactionId);
CREATE INDEX split_date_index ON split (date);
CREATE INDEX split_updated_index ON split (updated);
CREATE INDEX budgetitem_orgId_index ON budgetitem (orgId);

View File

@@ -1,105 +1,105 @@
package main
import (
"encoding/json"
"github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types"
"log"
"os"
"encoding/json"
"github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: migrate1.go <upgrade/downgrade>")
}
if len(os.Args) != 2 {
log.Fatal("Usage: migrate1.go <upgrade/downgrade>")
}
command := os.Args[1]
command := os.Args[1]
if command != "upgrade" && command != "downgrade" {
log.Fatal("Usage: migrate1.go <upgrade/downgrade>")
}
if command != "upgrade" && command != "downgrade" {
log.Fatal("Usage: migrate1.go <upgrade/downgrade>")
}
//filename is the path to the json config file
var config types.Config
file, err := os.Open("./config.json")
//filename is the path to the json config file
var config types.Config
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
connectionString := config.User + ":" + config.Password + "@/" + config.Database
db, err := db.NewDB(connectionString)
connectionString := config.User + ":" + config.Password + "@/" + config.Database
db, err := db.NewDB(connectionString)
if command == "upgrade" {
err = upgrade(db)
} else {
err = downgrade(db)
}
if command == "upgrade" {
err = upgrade(db)
} else {
err = downgrade(db)
}
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
log.Println("done")
log.Println("done")
}
func upgrade(db *db.DB) (err error) {
tx, err := db.Begin()
tx, err := db.Begin()
if err != nil {
return
}
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "ALTER TABLE user ADD COLUMN signupSource VARCHAR(100) NOT NULL AFTER emailVerifyCode"
query1 := "ALTER TABLE user ADD COLUMN signupSource VARCHAR(100) NOT NULL AFTER emailVerifyCode"
if _, err = tx.Exec(query1); err != nil {
return
}
if _, err = tx.Exec(query1); err != nil {
return
}
return
return
}
func downgrade(db *db.DB) (err error) {
tx, err := db.Begin()
tx, err := db.Begin()
if err != nil {
return
}
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "ALTER TABLE user DROP COLUMN signupSource"
query1 := "ALTER TABLE user DROP COLUMN signupSource"
if _, err = tx.Exec(query1); err != nil {
return
}
if _, err = tx.Exec(query1); err != nil {
return
}
return
return
}

View File

@@ -1,105 +1,105 @@
package main
import (
"encoding/json"
"github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types"
"log"
"os"
"encoding/json"
"github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: migrate2.go <upgrade/downgrade>")
}
if len(os.Args) != 2 {
log.Fatal("Usage: migrate2.go <upgrade/downgrade>")
}
command := os.Args[1]
command := os.Args[1]
if command != "upgrade" && command != "downgrade" {
log.Fatal("Usage: migrate2.go <upgrade/downgrade>")
}
if command != "upgrade" && command != "downgrade" {
log.Fatal("Usage: migrate2.go <upgrade/downgrade>")
}
//filename is the path to the json config file
var config types.Config
file, err := os.Open("./config.json")
//filename is the path to the json config file
var config types.Config
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
connectionString := config.User + ":" + config.Password + "@/" + config.Database
db, err := db.NewDB(connectionString)
connectionString := config.User + ":" + config.Password + "@/" + config.Database
db, err := db.NewDB(connectionString)
if command == "upgrade" {
err = upgrade(db)
} else {
err = downgrade(db)
}
if command == "upgrade" {
err = upgrade(db)
} else {
err = downgrade(db)
}
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
log.Println("done")
log.Println("done")
}
func upgrade(db *db.DB) (err error) {
tx, err := db.Begin()
tx, err := db.Begin()
if err != nil {
return
}
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "ALTER TABLE org ADD COLUMN timezone VARCHAR(100) NOT NULL AFTER `precision`"
query1 := "ALTER TABLE org ADD COLUMN timezone VARCHAR(100) NOT NULL AFTER `precision`"
if _, err = tx.Exec(query1); err != nil {
return
}
if _, err = tx.Exec(query1); err != nil {
return
}
return
return
}
func downgrade(db *db.DB) (err error) {
tx, err := db.Begin()
tx, err := db.Begin()
if err != nil {
return
}
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "ALTER TABLE org DROP COLUMN timezone"
query1 := "ALTER TABLE org DROP COLUMN timezone"
if _, err = tx.Exec(query1); err != nil {
return
}
if _, err = tx.Exec(query1); err != nil {
return
}
return
return
}

105
migrations/migrate3.go Normal file
View File

@@ -0,0 +1,105 @@
package main
import (
"encoding/json"
"github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: migrate3.go <upgrade/downgrade>")
}
command := os.Args[1]
if command != "upgrade" && command != "downgrade" {
log.Fatal("Usage: migrate3.go <upgrade/downgrade>")
}
//filename is the path to the json config file
var config types.Config
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
connectionString := config.User + ":" + config.Password + "@/" + config.Database
db, err := db.NewDB(connectionString)
if command == "upgrade" {
err = upgrade(db)
} else {
err = downgrade(db)
}
if err != nil {
log.Fatal(err)
}
log.Println("done")
}
func upgrade(db *db.DB) (err error) {
tx, err := db.Begin()
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "CREATE TABLE budgetitem (id INT UNSIGNED NOT NULL AUTO_INCREMENT, orgId BINARY(16) NOT NULL, accountId BINARY(16) NOT NULL, inserted BIGINT UNSIGNED NOT NULL, amount BIGINT NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;"
if _, err = tx.Exec(query1); err != nil {
return
}
return
}
func downgrade(db *db.DB) (err error) {
tx, err := db.Begin()
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit()
}
}()
query1 := "DROP TABLE budgetitem"
if _, err = tx.Exec(query1); err != nil {
return
}
return
}

View File

@@ -29,3 +29,5 @@ CREATE TABLE session (id BINARY(16) NOT NULL, inserted BIGINT UNSIGNED NOT NULL,
CREATE TABLE apikey (id BINARY(16) NOT NULL, inserted BIGINT UNSIGNED NOT NULL, updated BIGINT UNSIGNED NOT NULL, userId BINARY(16) NOT NULL, label VARCHAR(300) NOT NULL, deleted BIGINT UNSIGNED, PRIMARY KEY(id)) ENGINE=InnoDB;
CREATE TABLE invite (id VARCHAR(32) NOT NULL, orgId BINARY(16) NOT NULL, inserted BIGINT UNSIGNED NOT NULL, updated BIGINT UNSIGNED NOT NULL, email VARCHAR(100) NOT NULL, accepted BOOLEAN NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;
CREATE TABLE budgetitem (id INT UNSIGNED NOT NULL AUTO_INCREMENT, orgId BINARY(16) NOT NULL, accountId BINARY(16) NOT NULL, inserted BIGINT UNSIGNED NOT NULL, amount BIGINT NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;