diff --git a/database/migrations/20200323000437-create-bundle-codes-table.js b/database/migrations/20200323000437-create-bundle-codes-table.js new file mode 100644 index 0000000000000000000000000000000000000000..056e525a787e45646a0c697e636d942da65f3038 --- /dev/null +++ b/database/migrations/20200323000437-create-bundle-codes-table.js @@ -0,0 +1,49 @@ +module.exports = { + up: (migration, Types) => { + return migration.createTable('bundle_codes', { + id: { + type: Types.UUID, + primaryKey: true, + defaultValue: Types.UUIDV4, + allowNull: false, + }, + name: { + type: Types.TEXT, + allowNull: false, + }, + description: { + type: Types.TEXT, + allowNull: false, + }, + platforms: { + type: Types.JSONB, + defaultValue: {}, + allowNull: false, + }, + meta: { + type: Types.JSONB, + defaultValue: {}, + allowNull: false, + }, + created_at: { + type: Types.DATE, + defaultValue: Types.fn('now'), + allowNull: false, + }, + updated_at: { + type: Types.DATE, + defaultValue: Types.fn('now'), + allowNull: false, + }, + deleted_at: { + type: Types.DATE, + defaultValue: null, + allowNull: true, + }, + }) + }, + + down: (migration, Types) => { + return migration.dropTable('') + }, +} \ No newline at end of file diff --git a/database/migrations/20200323000553-create-user-bundle-codes-table.js b/database/migrations/20200323000553-create-user-bundle-codes-table.js new file mode 100644 index 0000000000000000000000000000000000000000..84b0443be83e4e9c405463d84e5a32162028c6d3 --- /dev/null +++ b/database/migrations/20200323000553-create-user-bundle-codes-table.js @@ -0,0 +1,30 @@ +module.exports = { + up: (migration, Types) => { + return migration.createTable('user_bundle_codes', { + user_id: { + type: Types.UUID, + primaryKey: true, + references: { + model: 'users', + key: 'id', + }, + onDelete: 'cascade', + onUpdate: 'cascade', + }, + bundle_code_id: { + type: Types.UUID, + primaryKey: true, + references: { + model: 'bundle_codes', + key: 'id', + }, + onDelete: 'cascade', + onUpdate: 'cascade', + }, + }) + }, + + down: (migration, Types) => { + return migration.dropTable('user_bundle_codes') + }, +} \ No newline at end of file diff --git a/package.json b/package.json index f5fd0bba76ce7f469fce65e94dffb722fac7ab6f..962b3dd7d4c9529036eda47f69979de2e10ae51c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jetsam-server", - "version": "1.2.0", + "version": "1.3.0", "description": "", "main": "index.js", "scripts": { diff --git a/src/database/models/BundleCode.js b/src/database/models/BundleCode.js new file mode 100644 index 0000000000000000000000000000000000000000..d97a6e957b9f3643c053343ef1e02cb5b4abf4b4 --- /dev/null +++ b/src/database/models/BundleCode.js @@ -0,0 +1,55 @@ +const timestamps = require('./properties/timestamps') + +module.exports = (sequelize, DataTypes) => { + const Model = sequelize.define('BundleCode', Object.assign( + { + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: DataTypes.UUIDV4, + validate: { + isUUID: 4, + }, + }, + name: { + type: DataTypes.TEXT, + }, + description: { + type: DataTypes.TEXT, + }, + platforms: { + type: DataTypes.JSONB, + }, + meta: { + type: DataTypes.JSONB, + }, + }, + timestamps(DataTypes), + ), { + paranoid: true, + tableName: 'bundle_codes', + }) + + Model.getPolyIdentifier = () => 'bundle_code' + Model.getRelationIdentifier = () => 'bundle_codes' + + Model.prototype.toJSON = function userToJSON() { + return { + id: this.id, + name: this.name, + description: this.description, + platforms: this.platforms, + meta: this.meta, + created_at: this.created_at, + updated_at: this.updated_at, + } + } + + Model.associate = function defineModelAssociations(models) { + Model.belongsToMany(models.User, { through: 'user_bundle_codes', foreignKey: 'bundle_code_id', otherKey: 'user_id', timestamps: false }) + } + + Model.relations = [] + + return Model +} \ No newline at end of file diff --git a/src/database/models/User.js b/src/database/models/User.js index 11460d0523b20b629c9448f4d5df6b44c48e57c1..37ddcea1bc36e10d1bc14c06b2450df02f544c93 100644 --- a/src/database/models/User.js +++ b/src/database/models/User.js @@ -142,6 +142,7 @@ module.exports = (sequelize, DataTypes) => { Model.hasMany(models.RefreshToken, { foreignKey: 'user_id' }) Model.hasMany(models.File, { foreignKey: 'user_id' }) Model.belongsToMany(models.File, { as: 'likes', through: 'user_file_likes', foreignKey: 'user_id', otherKey: 'file_id' }) + Model.belongsToMany(models.BundleCode, { through: 'user_bundle_codes', foreignKey: 'user_id', otherKey: 'bundle_code_id', timestamps: false }) } Model.relations = [ diff --git a/src/domain/authentication/AuthService.js b/src/domain/authentication/AuthService.js index 3e3737477817725d859d0f52f0b5e83941995ee6..1064a850ab81f8b8024d4edb05ab7cb257146abd 100644 --- a/src/domain/authentication/AuthService.js +++ b/src/domain/authentication/AuthService.js @@ -58,7 +58,9 @@ module.exports = class AuthService extends ContextualModule { } } else if (this.ctx.get('Authorization')) { const token = this.ctx.get('Authorization').substr(BEARER_PREFIX.length) + console.log("TOKEN", token) const user = await this.ctx.services.userService.findByAccessToken(token) + console.log(user) if (user) { this.authenticateAs(user) return user diff --git a/src/http/controllers/api/app.js b/src/http/controllers/api/app.js new file mode 100644 index 0000000000000000000000000000000000000000..8fd48c7e3465deb041b022f3727db608946d509e --- /dev/null +++ b/src/http/controllers/api/app.js @@ -0,0 +1,15 @@ +exports.getBundles = async ctx => { + const user = await ctx.services.authService.getUser() + if (user == null) { + ctx.body = { + bundles: [], + } + } else { + console.log(user.id) + + const bundles = await user.getBundleCodes() + ctx.body = { + bundles, + } + } +} \ No newline at end of file diff --git a/src/http/controllers/api/auth.js b/src/http/controllers/api/auth.js index bd708c9f556e5923f1aa5ef3d456cbabac198b00..e26f99a910a95d52306165ffe07f726f64eda5e3 100644 --- a/src/http/controllers/api/auth.js +++ b/src/http/controllers/api/auth.js @@ -61,7 +61,7 @@ exports.triggerPasswordReset = async ctx => { } ctx.body = { - reset_token: token, + reset_token: null, } } diff --git a/src/http/routes.js b/src/http/routes.js index 18c3a8677173c2485d43a86115025259bba588fa..4830deca6488ef05152f7c930e8e2bed8e9b71fd 100644 --- a/src/http/routes.js +++ b/src/http/routes.js @@ -50,6 +50,7 @@ function mount(api) { api.post('/auth/reset-password', controller('api/auth', 'handlePasswordReset')) api.get('/self', controller('api/user', 'self')) + api.get('/self/bundles', controller('api/app', 'getBundles')) api.put('/self/:property', controller('api/user', 'updateOne')) api.post('/feedback', controller('api/feedback', 'send'))