Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. 'use strict';
  2. var config = require('../config.json');
  3. var path = require('path');
  4. var loopback = require('loopback');
  5.  
  6. module.exports = function(Advertiser) {
  7. Advertiser.registration = function(address, company, companyDescription, serviceType, vat, fullName, email, password, cb) {
  8. var app = Advertiser.app;
  9. var advertiserUser = app.models.advertiserUser;
  10. var Role = app.models.Role;
  11. var RoleMapping = app.models.RoleMapping;
  12.  
  13. Advertiser.create({
  14. address: address,
  15. company: company,
  16. companyDescription: companyDescription,
  17. serviceType: serviceType,
  18. vat: vat
  19. })
  20. .then(function (result) {
  21. Advertiser.createUser(result, fullName, email, password, cb);
  22. })
  23. .catch(function (err) {
  24. console.log('Error from registration.', err);
  25. })
  26.  
  27. }
  28.  
  29. // method for creaation of user with role advertiser
  30. Advertiser.createUser = function (advertiser, fullName, email, password, cb) {
  31. var app = Advertiser.app;
  32. var advertiserUser = app.models.advertiserUser;
  33. var Role = app.models.Role;
  34. var RoleMapping = app.models.RoleMapping;
  35.  
  36. advertiserUser.create({
  37. advertiserId: advertiser.id,
  38. fullName: fullName,
  39. email: email,
  40. password: password
  41. }).then(function (user) {
  42. // find role advertiser and assign it to advertiser
  43. Role.findOne({ where: { name: 'role-advertiser' } }).then(function (role) {
  44. // var id = String(user.id);
  45. role.principals.create({
  46. principalType: RoleMapping.USER,
  47. principalId: user.id
  48. }, function(err, principal) {
  49. if (err) throw err;
  50. }, cb(null, user))
  51. Advertiser.createCont(advertiser);
  52. })
  53. .catch(function (err) {
  54. console.log('Error role principal', err);
  55. });
  56. })
  57. .catch(function(err) {
  58. console.log('Error advertiserUser create', err);
  59. })
  60.  
  61. }
  62.  
  63. // method for creation of image container
  64. Advertiser.createCont = function (advertiser, cb) {
  65. var app = Advertiser.app;
  66. var Container = app.models.container;
  67. var id = String(advertiser.id);
  68. Container.createContainer({
  69. name: id
  70. }, function(result) {
  71.  
  72. });
  73. }
  74.  
  75. Advertiser.remoteMethod('registration', {
  76. description: 'register advertiser and creates userAdvertiser with role advertiser',
  77. accepts: [
  78. {arg: 'address', type: 'object', required: true},
  79. {arg: 'company', type: 'string', required: true},
  80. {arg: 'companyDescription', type: 'string', required: true},
  81. {arg: 'serviceType', type: 'string', required: true},
  82. {arg: 'vat', type: 'string', required: true},
  83. {arg: 'fullName', type: 'string', required: true},
  84. {arg: 'email', type: 'string', required: true},
  85. {arg: 'password', type: 'string', required: true},
  86. ],
  87. returns: {arg: 'user', type: 'object'},
  88. http: {verb: 'post', path: '/registration'}
  89. });
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement