mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
WIP Items module.
This commit is contained in:
16
server/src/models/Account.js
Normal file
16
server/src/models/Account.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Account = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'accounts',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
});
|
||||
|
||||
export default bookshelf.model('Account', Account);
|
||||
0
server/src/models/AccountBalance.js
Normal file
0
server/src/models/AccountBalance.js
Normal file
0
server/src/models/AccountType.js
Normal file
0
server/src/models/AccountType.js
Normal file
30
server/src/models/Item.js
Normal file
30
server/src/models/Item.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Item = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'items',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
|
||||
/**
|
||||
* Item may has many meta data.
|
||||
*/
|
||||
metadata() {
|
||||
return this.hasMany('ItemMetadata', 'item_id');
|
||||
},
|
||||
|
||||
/**
|
||||
* Item may belongs to the item category.
|
||||
*/
|
||||
category() {
|
||||
return this.belongsTo('ItemCategory', 'category_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('Item', Item);
|
||||
23
server/src/models/ItemCategory.js
Normal file
23
server/src/models/ItemCategory.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const ItemCategory = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'items_categories',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
|
||||
/**
|
||||
* Item category may has many items.
|
||||
*/
|
||||
items() {
|
||||
return this.hasMany('Item', 'category_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('ItemCategory', ItemCategory);
|
||||
23
server/src/models/ItemMetadata.js
Normal file
23
server/src/models/ItemMetadata.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const ItemMetadata = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'items_metadata',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
|
||||
/**
|
||||
* Item category may has many items.
|
||||
*/
|
||||
items() {
|
||||
return this.belongsTo('Item', 'item_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('ItemMetadata', ItemMetadata);
|
||||
16
server/src/models/OAuthClient.js
Normal file
16
server/src/models/OAuthClient.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const OAuthClient = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'oauth_clients',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
});
|
||||
|
||||
export default bookshelf.model('OAuthClient', OAuthClient);
|
||||
81
server/src/models/OAuthServerModel.js
Normal file
81
server/src/models/OAuthServerModel.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import OAuthClient from '@/models/OAuthClient';
|
||||
import OAuthToken from '@/models/OAuthToken';
|
||||
import User from '@/models/User';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* Retrieve the access token.
|
||||
* @param {String} bearerToken -
|
||||
*/
|
||||
async getAccessToken(bearerToken) {
|
||||
const token = await OAuthClient.where({
|
||||
access_token: bearerToken,
|
||||
}).fetch();
|
||||
|
||||
return {
|
||||
accessToken: token.attributes.access_token,
|
||||
client: {
|
||||
id: token.attributes.client_id,
|
||||
},
|
||||
expires: token.attributes.access_token_expires_on,
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieve the client from client id and secret.
|
||||
* @param {Number} clientId -
|
||||
* @param {String} clientSecret -
|
||||
*/
|
||||
async getClient(clientId, clientSecret) {
|
||||
const token = await OAuthClient.where({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
});
|
||||
|
||||
if (!token) { return {}; }
|
||||
|
||||
return {
|
||||
clientId: token.attributes.client_id,
|
||||
clientSecret: token.attributes.client_secret,
|
||||
grants: ['password'],
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Get specific user with given username and password.
|
||||
*/
|
||||
async getUser(username, password) {
|
||||
const user = await User.query((query) => {
|
||||
query.where('username', username);
|
||||
query.where('password', password);
|
||||
}).fetch();
|
||||
|
||||
return {
|
||||
...user.attributes,
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Saves the access token.
|
||||
* @param {Object} token -
|
||||
* @param {Object} client -
|
||||
* @param {Object} user -
|
||||
*/
|
||||
async saveAccessToken(token, client, user) {
|
||||
const oauthToken = OAuthToken.forge({
|
||||
access_token: token.accessToken,
|
||||
access_token_expires_on: token.accessTokenExpiresOn,
|
||||
client_id: client.id,
|
||||
refresh_token: token.refreshToken,
|
||||
refresh_token_expires_on: token.refreshTokenExpiresOn,
|
||||
user_id: user.id,
|
||||
});
|
||||
|
||||
await oauthToken.save();
|
||||
|
||||
return {
|
||||
client: { id: client.id },
|
||||
user: { id: user.id },
|
||||
};
|
||||
},
|
||||
};
|
||||
16
server/src/models/OAuthToken.js
Normal file
16
server/src/models/OAuthToken.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const OAuthToken = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'oauth_tokens',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
});
|
||||
|
||||
export default bookshelf.model('OAuthToken', OAuthToken);
|
||||
16
server/src/models/PasswordReset.js
Normal file
16
server/src/models/PasswordReset.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const PasswordResets = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'password_resets',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
});
|
||||
|
||||
export default bookshelf.model('PasswordResets', PasswordResets);
|
||||
31
server/src/models/Role.js
Normal file
31
server/src/models/Role.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Role = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name of Role model.
|
||||
* @type {String}
|
||||
*/
|
||||
tableName: 'roles',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
|
||||
/**
|
||||
* Role may has many permissions.
|
||||
*/
|
||||
permissions() {
|
||||
return this.belongsToMany('Permission', 'role_has_permissions', 'role_id', 'permission_id');
|
||||
},
|
||||
|
||||
/**
|
||||
* Role model may has many users.
|
||||
*/
|
||||
users() {
|
||||
return this.belongsTo('User');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('Role', Role);
|
||||
16
server/src/models/Setting.js
Normal file
16
server/src/models/Setting.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Setting = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'settings',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
});
|
||||
|
||||
export default bookshelf.model('Setting', Setting);
|
||||
26
server/src/models/User.js
Normal file
26
server/src/models/User.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const User = bookshelf.Model.extend({
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'users',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
|
||||
/**
|
||||
* Verify the password of the user.
|
||||
* @param {String} password - The given password.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
verifyPassword(password) {
|
||||
return bcrypt.compareSync(password, this.get('password'));
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('User', User);
|
||||
17
server/src/models/bookshelf.js
Normal file
17
server/src/models/bookshelf.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import Bookshelf from 'bookshelf';
|
||||
import jsonColumns from 'bookshelf-json-columns';
|
||||
import bookshelfParanoia from 'bookshelf-paranoia';
|
||||
import bookshelfModelBase from 'bookshelf-modelbase';
|
||||
import knex from '../database/knex';
|
||||
|
||||
const bookshelf = Bookshelf(knex);
|
||||
|
||||
bookshelf.plugin('pagination');
|
||||
bookshelf.plugin('visibility');
|
||||
bookshelf.plugin('registry');
|
||||
bookshelf.plugin('virtuals');
|
||||
bookshelf.plugin(jsonColumns);
|
||||
bookshelf.plugin(bookshelfParanoia);
|
||||
bookshelf.plugin(bookshelfModelBase.pluggable);
|
||||
|
||||
export default bookshelf;
|
||||
Reference in New Issue
Block a user