WIP Items module.

This commit is contained in:
Ahmed Bouhuolia
2019-09-03 02:07:28 +02:00
parent cb8c294d74
commit 70809cb05c
142 changed files with 12674 additions and 64 deletions

View 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);

View File

View File

30
server/src/models/Item.js Normal file
View 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);

View 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);

View 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);

View 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);

View 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 },
};
},
};

View 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);

View 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
View 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);

View 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
View 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);

View 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;