News.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. DataTypes,
  3. Model,
  4. InferAttributes,
  5. InferCreationAttributes,
  6. CreationOptional,
  7. ForeignKey,
  8. NonAttribute,
  9. BelongsToCreateAssociationMixin,
  10. BelongsToGetAssociationMixin,
  11. BelongsToSetAssociationMixin,
  12. Association
  13. } from "sequelize";
  14. import { NewsStatus } from "@models/News/NewsStatus";
  15. import EventsModule from "@/modules/EventsModule";
  16. import User from "./User";
  17. import { ObjectIdType } from "@/modules/DataModule";
  18. export class News extends Model<
  19. // eslint-disable-next-line no-use-before-define
  20. InferAttributes<News>,
  21. // eslint-disable-next-line no-use-before-define
  22. InferCreationAttributes<News>
  23. > {
  24. declare _id: CreationOptional<ObjectIdType>;
  25. declare title: string;
  26. declare markdown: string;
  27. declare status: CreationOptional<NewsStatus>;
  28. declare showToNewUsers: CreationOptional<boolean>;
  29. declare createdBy:
  30. | ForeignKey<User["_id"]>
  31. | {
  32. _id: ForeignKey<User["_id"]>;
  33. _name: "minifiedUsers";
  34. };
  35. declare createdAt: CreationOptional<Date>;
  36. declare updatedAt: CreationOptional<Date>;
  37. declare getCreatedByModel: BelongsToGetAssociationMixin<User>;
  38. declare setCreatedByModel: BelongsToSetAssociationMixin<User, number>;
  39. declare createCreatedByModel: BelongsToCreateAssociationMixin<User>;
  40. declare createdByModel?: NonAttribute<User>;
  41. declare static associations: {
  42. // eslint-disable-next-line no-use-before-define
  43. createdByModel: Association<News, User>;
  44. };
  45. }
  46. export const schema = {
  47. _id: {
  48. type: DataTypes.OBJECTID,
  49. allowNull: false,
  50. primaryKey: true
  51. },
  52. title: {
  53. type: DataTypes.STRING,
  54. allowNull: false
  55. },
  56. markdown: {
  57. type: DataTypes.TEXT,
  58. allowNull: false
  59. },
  60. status: {
  61. type: DataTypes.ENUM(...Object.values(NewsStatus)),
  62. defaultValue: NewsStatus.DRAFT,
  63. allowNull: false
  64. },
  65. showToNewUsers: {
  66. type: DataTypes.BOOLEAN,
  67. defaultValue: false,
  68. allowNull: false
  69. },
  70. createdAt: DataTypes.DATE,
  71. updatedAt: DataTypes.DATE,
  72. _name: {
  73. type: DataTypes.VIRTUAL,
  74. get() {
  75. return "news";
  76. }
  77. },
  78. _associations: {
  79. type: DataTypes.VIRTUAL,
  80. get() {
  81. return {
  82. createdBy: "minifiedUsers"
  83. };
  84. }
  85. }
  86. };
  87. export const options = {};
  88. export const setup = async () => {
  89. News.belongsTo(User, {
  90. as: "createdByModel",
  91. foreignKey: {
  92. name: "createdBy",
  93. type: DataTypes.OBJECTID,
  94. allowNull: false
  95. },
  96. onDelete: "RESTRICT",
  97. onUpdate: "RESTRICT"
  98. });
  99. News.afterSave(async record => {
  100. const doc = record.get();
  101. const oldStatus = record.previous("status");
  102. const newStatus = record.get("status");
  103. if (oldStatus === newStatus) return;
  104. if (newStatus === NewsStatus.PUBLISHED) {
  105. const EventClass = EventsModule.getEvent(`data.news.published`);
  106. await EventsModule.publish(
  107. new EventClass({
  108. doc: record
  109. })
  110. );
  111. } else if (oldStatus === NewsStatus.PUBLISHED) {
  112. const EventClass = EventsModule.getEvent(`data.news.unpublished`);
  113. await EventsModule.publish(
  114. new EventClass(
  115. {
  116. oldDoc: {
  117. _id: doc._id.toString()
  118. }
  119. },
  120. doc._id.toString()
  121. )
  122. );
  123. }
  124. });
  125. News.afterDestroy(async record => {
  126. const doc = record.get();
  127. if (doc.status === NewsStatus.PUBLISHED) {
  128. const EventClass = EventsModule.getEvent(`data.news.unpublished`);
  129. await EventsModule.publish(
  130. new EventClass(
  131. {
  132. oldDoc: {
  133. _id: doc._id.toString()
  134. }
  135. },
  136. doc._id.toString()
  137. )
  138. );
  139. }
  140. });
  141. };
  142. export default News;