models.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='shop-roles') => {
  4. const {Savable, slice} = await connect(dbName)
  5. async function getModels({id, acl}){
  6. const SlicedSavable = slice(id === 'anon' ? [id] : [...acl])
  7. class User extends SlicedSavable {
  8. constructor(...params){
  9. super(...params)
  10. this.originalACL = this.acl && [...this.acl]
  11. }
  12. async save(...params){
  13. console.log(this)
  14. if (!this.login || !this.password){
  15. throw new Error(`Login and password are mandatory`)
  16. }
  17. let otherUserWithThisLogin = this.login && await Savable.m.User.findOne({login: this.login})
  18. if (this._id){
  19. if (otherUserWithThisLogin && otherUserWithThisLogin._id.toString() !== this._id.toString()){
  20. throw new ReferenceError(`User ${this.login} already exists`)
  21. }
  22. if (!acl.includes('admin') && this.originalACL.toString() !== this.acl.toString())
  23. throw new ReferenceError(`Not enough permissions for changing acl on ${this.login}`)
  24. else return await super.save(...params)
  25. }
  26. else {
  27. if (otherUserWithThisLogin){
  28. throw new ReferenceError(`User ${this.login} already exists`)
  29. }
  30. await super.save(...params)
  31. this.___owner = this._id.toString()
  32. this.acl = [this.___owner, "user"]
  33. return await Savable.prototype.save.call(this, ...params)
  34. }
  35. }
  36. static get relations(){ //don't needed due to ___owner in most cases
  37. return {
  38. avatar : "userAvatar",
  39. }
  40. }
  41. static get defaultPermissions(){
  42. return {
  43. create: ['anon'],
  44. read: ['owner', 'user', 'admin'],
  45. write: ['owner', 'admin'],
  46. delete: []
  47. }
  48. }
  49. async getACL(){
  50. return [this._id.toString(), "user"]
  51. }
  52. }
  53. SlicedSavable.addClass(User)
  54. class OwnerSlicedSavable extends SlicedSavable {
  55. get owner(){
  56. if (!this.___owner) return this.___owner
  57. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  58. }
  59. }
  60. class Image extends OwnerSlicedSavable {
  61. constructor(...params){
  62. super(...params)
  63. }
  64. static async fromFileData(fileData){
  65. if (!fileData) return
  66. let image = new Image({})
  67. image.fileData = fileData
  68. image.url = `images/${fileData.filename}`
  69. image.originalFileName = fileData.originalname
  70. await image.save()
  71. return image;
  72. }
  73. async save(...params){
  74. if (this.userAvatar){
  75. if (this.userAvatar._id.toString() !== id){
  76. throw new ReferenceError(`You can't set ava for other user`)
  77. }
  78. }
  79. return await super.save(...params)
  80. }
  81. static get relations(){
  82. return {
  83. userAvatar: "avatar", //if it is ava
  84. good: ["images"], //if it is ava
  85. category: "image", //if it is ava
  86. }
  87. }
  88. static get defaultPermissions(){
  89. return {
  90. create: ['user', 'admin'],
  91. read: ['anon', 'user', 'admin'],
  92. write: ['user', 'admin'],
  93. delete: ['admin']
  94. }
  95. }
  96. }
  97. SlicedSavable.addClass(Image)
  98. class Good extends OwnerSlicedSavable {
  99. constructor(...params){
  100. super(...params)
  101. }
  102. static get relations(){
  103. return {
  104. categories: ["goods"],
  105. orderGoods: "good",
  106. images: "good"
  107. }
  108. }
  109. static get defaultPermissions(){
  110. return {
  111. create: ['admin'],
  112. read: ['anon', 'user', 'admin'],
  113. write: ['admin'],
  114. delete: ['admin']
  115. }
  116. }
  117. static get guestRelations(){
  118. return ['orderGoods']
  119. }
  120. }
  121. SlicedSavable.addClass(Good)
  122. class Category extends SlicedSavable {
  123. constructor(...params){
  124. super(...params)
  125. }
  126. static get relations(){
  127. return {
  128. goods: ["categories"],
  129. image: "category",
  130. parent: ["subCategories"],
  131. subCategories: "parent"
  132. }
  133. }
  134. static get defaultPermissions(){
  135. return {
  136. create: ['admin'],
  137. read: ['anon', 'user', 'admin'],
  138. write: ['admin'],
  139. delete: ['admin']
  140. }
  141. }
  142. }
  143. SlicedSavable.addClass(Category)
  144. class Order extends OwnerSlicedSavable {
  145. constructor(...params){
  146. super(...params)
  147. }
  148. get total(){
  149. return (async () =>{
  150. let total = 0
  151. try {
  152. total = (await Promise.all(this.orderGoods)).reduce((a,b) => (a?.total || a) + (b?.total || 0), 0)
  153. }
  154. catch(e){
  155. }
  156. return total
  157. })()
  158. }
  159. static get relations(){
  160. return {
  161. orderGoods: "order"
  162. }
  163. }
  164. static get defaultPermissions(){
  165. return {
  166. //savable refs, objectid's, words like 'tags' or 'roles'
  167. read: ['owner', 'admin'],
  168. write: ['owner', 'admin'],
  169. create: ['user'],
  170. delete: [],
  171. /*permission
  172. * TODO: permissions for read and write permissions
  173. *
  174. */
  175. }
  176. }
  177. }
  178. SlicedSavable.addClass(Order)
  179. class OrderGood extends OwnerSlicedSavable {
  180. constructor(...params){
  181. super(...params)
  182. }
  183. get total(){
  184. return this.price*this.count
  185. }
  186. async save(...params){
  187. if (!this.good){
  188. throw new Error('Good Required for OrderGood')
  189. }
  190. if (!this.price && this.good && this.good.price){
  191. this.price = this.good.price
  192. this.goodName = this.good.name
  193. }
  194. return await super.save(...params)
  195. }
  196. static get relations(){
  197. return {
  198. good: ["orderGoods"],
  199. order: ["orderGoods"]
  200. }
  201. }
  202. static get defaultPermissions(){
  203. return {
  204. //savable refs, objectid's, words like 'tags' or 'roles'
  205. read: ['owner', 'admin'],
  206. write: ['owner', 'admin'],
  207. create: ['user'],
  208. delete: [],
  209. /*permission
  210. * TODO: permissions for read and write permissions
  211. *
  212. */
  213. }
  214. }
  215. }
  216. SlicedSavable.addClass(OrderGood)
  217. const thisUser = id !== 'anon' && await Savable.m.User.findOne({_id: ObjectID(id)})
  218. return {models: {
  219. SlicedSavable, ...SlicedSavable.classes
  220. },
  221. thisUser}
  222. }
  223. return {
  224. Savable,
  225. slice,
  226. getModels
  227. }
  228. }