site.coffee 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. module.exports = {
  2. _id: '_design/site',
  3. version: '1.0.0',
  4. appVersion: '1.0.0',
  5. hash: 'site_v1_0_0_' + Date.now(),
  6. views: {
  7. # Активные товары для каталога
  8. active_products: {
  9. map: ((doc) ->
  10. if doc.type == 'product' && doc.active == true
  11. emit([doc.category, doc.name], {
  12. _id: doc._id,
  13. name: doc.name,
  14. price: doc.price,
  15. oldPrice: doc.oldPrice,
  16. sku: doc.sku,
  17. image: doc.image,
  18. category: doc.category,
  19. description: doc.description,
  20. attributes: doc.attributes
  21. })
  22. ).toString()
  23. },
  24. # Опубликованные статьи блога
  25. published_articles: {
  26. map: ((doc) ->
  27. if doc.type == 'blog_article' && doc.published == true
  28. emit([doc.createdAt], {
  29. _id: doc._id,
  30. title: doc.title,
  31. slug: doc.slug,
  32. excerpt: doc.excerpt,
  33. image: doc.image,
  34. author: doc.author,
  35. createdAt: doc.createdAt,
  36. content: doc.content
  37. })
  38. ).toString()
  39. },
  40. # Активные слайды
  41. active_slides: {
  42. map: ((doc) ->
  43. if doc.type == 'hero_slide' && doc.active == true
  44. emit(doc.order, {
  45. _id: doc._id,
  46. title: doc.title,
  47. subtitle: doc.subtitle,
  48. image: doc.image,
  49. buttonText: doc.buttonText,
  50. buttonLink: doc.buttonLink
  51. })
  52. ).toString()
  53. }
  54. },
  55. validates_doc_update: ((newDoc, oldDoc, userCtx) ->
  56. # Базовая валидация документов
  57. # Запрещаем изменение design документов
  58. if newDoc._id && newDoc._id.startsWith('_design/')
  59. if oldDoc # existing document
  60. throw { forbidden: 'Design documents can only be updated by admins' }
  61. # Валидация товаров
  62. if newDoc.type == 'product'
  63. if !newDoc.name
  64. throw { forbidden: 'Product must have a name' }
  65. if !newDoc.price || isNaN(parseFloat(newDoc.price))
  66. throw { forbidden: 'Product must have a valid price' }
  67. if !newDoc.sku
  68. throw { forbidden: 'Product must have SKU' }
  69. # Валидация статей блога
  70. if newDoc.type == 'blog_article'
  71. if !newDoc.title
  72. throw { forbidden: 'Blog article must have a title' }
  73. if !newDoc.slug
  74. throw { forbidden: 'Blog article must have a slug' }
  75. return true
  76. ).toString()
  77. }