Guest User

Untitled

a guest
Dec 6th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const keystone = require('keystone');
  4. const Types = keystone.Field.Types;
  5. let Country = keystone.list('Country');
  6.  
  7. /**
  8. * User Model
  9. * ==========
  10. */
  11. let User = new keystone.List('User', {history: true});
  12.  
  13. User.add({
  14. name: {type: Types.Name, required: true, index: true},
  15. email: {type: Types.Email, initial: true, required: true, unique: true, index: true},
  16. password: {type: Types.Password, initial: true, required: false},
  17. phone: {type: Types.Text, unique: true},
  18. avatar: {type: Types.Text},
  19. registrationEnd: {type: Types.Date},
  20. registrationStart: {type: Types.Date},
  21. utitle: {type: Types.Relationship, ref: 'Title'},
  22. dob: {type: Types.Date, format: 'DD/MM/YYYY'},
  23. permAddress: {
  24. type: Types.Location,
  25. state: {type: Types.Relationship, ref: 'State'},
  26. country: {type: Types.Relationship, ref: 'Country'}
  27. },
  28. sor: {type: Types.Relationship, ref: 'State'},
  29. lga: {type: Types.Relationship, ref: 'LGA'},
  30. religion: {type: Types.Relationship, ref: 'Religion'},
  31. marital: {type: Types.Relationship, ref: 'Marital'},
  32. sex: {type: Types.Select, options: 'male, female'},
  33. education: {type: Types.Relationship, ref: 'Education', dependsOn: {isClient: true}},
  34. seenLawyer: {type: Boolean, default: false, dependsOn: {isClient: true}},
  35. occupation: {type: Types.Relationship, ref: 'Occupation', dependsOn: {isClient: true}},
  36. firm: {type: Types.Text, dependsOn: {isLawyer: true}},
  37. lawyerSince: {type: Types.Date, dependsOn: {isLawyer: true}},
  38. areas: {type: Types.Relationship, ref: 'LawArea', default: false, many: true, dependsOn: {isLawyer: true}},
  39. }, 'Permissions', {
  40. isAdmin: {type: Boolean, label: 'Can access Keystone', index: true, default: false},
  41. isLawyer: {type: Boolean, index: true, default: false},
  42. isClient: {type: Boolean, index: true, default: false},
  43. });
  44. User.schema.pre('save', function (next) {
  45. let thisUser = this;
  46. Country.model.findOne({name: 'Denmark'}).then((result) => {
  47. thisUser.permAddress.country = result._id;
  48. next();
  49. });
  50. });
  51. User.schema.virtual('canAccessKeystone').get(function () {
  52. return this.isAdmin;
  53. });
  54. User.schema.virtual('law').get(function () {
  55. return this.isLawyer;
  56. });
  57. User.schema.virtual('client').get(function () {
  58. return this.isClient;
  59. });
  60. User.schema.virtual('fullname').get(function () {
  61. return this.name.first + ' ' + this.name.last;
  62. });
  63. // Return only a user's _id
  64. User.statics = {
  65. getUserId: function (email, cb) {
  66. this.findOne({email: email}).select('_id').exec(cb);
  67. }
  68. };
  69. User.defaultColumns = 'name, email, isLawyer, isAdmin';
  70. User.register();
  71.  
  72. view.on('init', function (next) {
  73. console.log(req.user.email);
  74. keystone.list('User').model.findOne({email: req.user.email}).populate({
  75. path: 'utitle',
  76. select: 'name'
  77. }).populate({path: 'sor', select: 'name'}).populate({path: 'lga', select: 'name'}).populate({
  78. path: 'religion',
  79. select: 'name'
  80. }).populate({path: 'marital', select: 'name'}).populate({
  81. path: 'education',
  82. select: 'name'
  83. }).populate({path: 'occupation', select: 'name'}).exec(function (err, results) {
  84.  
  85. if (err || !results.length) {
  86. return next(err);
  87. }
  88.  
  89. console.log(results); // Expect to find populated documents
  90. locals.user = results; next(err);
  91.  
  92. });
  93. });
  94.  
  95. locals.clientType = 'Free';
  96. locals.profilelink = '/portal/profile';
  97.  
  98. // Render the view
  99. if (link === undefined) {
  100. view.render('portal/client', {layout: 'portal'});
  101. } else {
  102. view.render('client/' + link, {layout: 'portal'});
  103. }
  104.  
  105. { name: { first: 'Mike', last: 'Mikey' },
  106. permAddress:
  107. { country: '5c08562d6ca7fa009b16d2ce',
  108. postcode: '220282',
  109. state: '5c08562d6ca7fa009b16d2d8',
  110. street1: '22, Ajanlekoko Street, ',
  111. suburb: 'Abule-Egba' },
  112. seenLawyer: false,
  113. areas: [],
  114. isAdmin: false,
  115. isLawyer: false,
  116. isClient: true,
  117. _id: 5c08562e6ca7fa009b16d310,
  118. email: 'xxxxxx@xxx.xx',
  119. password:
  120. 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  121. phone: '0703268xxxx',
  122. __rev: 3,
  123. __v: 0,
  124. avatar: '',
  125. dob: 2018-12-04T23:00:00.000Z,
  126. registrationEnd: 2018-12-04T23:00:00.000Z,
  127. registrationStart: 2018-12-04T23:00:00.000Z,
  128. utitle: 5c08562e6ca7fa009b16d2ee,
  129. education: 5c08562e6ca7fa009b16d2f9,
  130. lga: 5c08562d6ca7fa009b16d2db,
  131. marital: 5c08562e6ca7fa009b16d2e5,
  132. occupation: 5c08562e6ca7fa009b16d309,
  133. religion: 5c08562e6ca7fa009b16d2e0,
  134. sex: 'female',
  135. sor: 5c08562d6ca7fa009b16d2d8 }
Add Comment
Please, Sign In to add comment