@winible/winible-typed 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/README.md +32 -0
  2. package/dist/index.js +20 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/migrations/20220601151258-custom_name_describing_your_migration.js +19 -0
  5. package/dist/migrations/20220601151258-custom_name_describing_your_migration.js.map +1 -0
  6. package/dist/migrations/20220605225655-create-dashboard-user-permissions.js +27 -0
  7. package/dist/migrations/20220605225655-create-dashboard-user-permissions.js.map +1 -0
  8. package/dist/migrations/20220606001300-update-dashboard-user-permissions.js +14 -0
  9. package/dist/migrations/20220606001300-update-dashboard-user-permissions.js.map +1 -0
  10. package/dist/migrations/20220607091109-add-read-date-for-message.js +19 -0
  11. package/dist/migrations/20220607091109-add-read-date-for-message.js.map +1 -0
  12. package/dist/migrations/20220616225456-add-delete-columns.js +21 -0
  13. package/dist/migrations/20220616225456-add-delete-columns.js.map +1 -0
  14. package/dist/migrations/20220709202211-disable-feed.js +24 -0
  15. package/dist/migrations/20220709202211-disable-feed.js.map +1 -0
  16. package/dist/migrations/20220723215213-limit-promotion-claims.js +18 -0
  17. package/dist/migrations/20220723215213-limit-promotion-claims.js.map +1 -0
  18. package/dist/migrations/20220723220233-num_claims.js +12 -0
  19. package/dist/migrations/20220723220233-num_claims.js.map +1 -0
  20. package/dist/migrations/20220723221907-default_false_promos.js +17 -0
  21. package/dist/migrations/20220723221907-default_false_promos.js.map +1 -0
  22. package/dist/migrations/20220724172559-default_zero_claims.js +13 -0
  23. package/dist/migrations/20220724172559-default_zero_claims.js.map +1 -0
  24. package/dist/migrations/20220724201419-pinned_post.js +13 -0
  25. package/dist/migrations/20220724201419-pinned_post.js.map +1 -0
  26. package/dist/migrations/20220729164625-unsend_mass.js +12 -0
  27. package/dist/migrations/20220729164625-unsend_mass.js.map +1 -0
  28. package/dist/migrations/20220823033002-interval-unit.js +14 -0
  29. package/dist/migrations/20220823033002-interval-unit.js.map +1 -0
  30. package/dist/migrations/20220830193530-processorId.js +15 -0
  31. package/dist/migrations/20220830193530-processorId.js.map +1 -0
  32. package/dist/migrations/20220907121932-add-processor-to-sub.js +15 -0
  33. package/dist/migrations/20220907121932-add-processor-to-sub.js.map +1 -0
  34. package/dist/migrations/20220907212232-add-trans-id.js +12 -0
  35. package/dist/migrations/20220907212232-add-trans-id.js.map +1 -0
  36. package/dist/migrations/20220907220423-big-int-trans.js +12 -0
  37. package/dist/migrations/20220907220423-big-int-trans.js.map +1 -0
  38. package/dist/migrations/20220909161902-add-stale.js +13 -0
  39. package/dist/migrations/20220909161902-add-stale.js.map +1 -0
  40. package/dist/migrations/20220922000225-verified-spender.js +13 -0
  41. package/dist/migrations/20220922000225-verified-spender.js.map +1 -0
  42. package/dist/migrations/20220922212108-verified-spender-2.js +16 -0
  43. package/dist/migrations/20220922212108-verified-spender-2.js.map +1 -0
  44. package/dist/migrations/20221013223648-product-subscription-list.js +12 -0
  45. package/dist/migrations/20221013223648-product-subscription-list.js.map +1 -0
  46. package/dist/migrations/20221014001037-product-subscription-list.js +75 -0
  47. package/dist/migrations/20221014001037-product-subscription-list.js.map +1 -0
  48. package/dist/migrations/20221026055245-update-type.js +15 -0
  49. package/dist/migrations/20221026055245-update-type.js.map +1 -0
  50. package/dist/scripts/authorizeTransfer.js +188 -0
  51. package/dist/scripts/authorizeTransfer.js.map +1 -0
  52. package/dist/scripts/createDashboardLink.js +30 -0
  53. package/dist/scripts/createDashboardLink.js.map +1 -0
  54. package/dist/scripts/deleteMalformedImages.js +84 -0
  55. package/dist/scripts/deleteMalformedImages.js.map +1 -0
  56. package/dist/scripts/importMixpanelEvents.js +44 -0
  57. package/dist/scripts/importMixpanelEvents.js.map +1 -0
  58. package/dist/scripts/payoutSubscriptions.js +185 -0
  59. package/dist/scripts/payoutSubscriptions.js.map +1 -0
  60. package/dist/scripts/removeUnreferenceSubs.js +39 -0
  61. package/dist/scripts/removeUnreferenceSubs.js.map +1 -0
  62. package/dist/scripts/test.js +33 -0
  63. package/dist/scripts/test.js.map +1 -0
  64. package/dist/server/index.js +30 -0
  65. package/dist/server/index.js.map +1 -0
  66. package/dist/server/middlewares/slack-auth.js +40 -0
  67. package/dist/server/middlewares/slack-auth.js.map +1 -0
  68. package/dist/server/v1/bypassCreator.js +81 -0
  69. package/dist/server/v1/bypassCreator.js.map +1 -0
  70. package/dist/server/v1/cancelSubscription.js +35 -0
  71. package/dist/server/v1/cancelSubscription.js.map +1 -0
  72. package/dist/server/v1/changeName.js +41 -0
  73. package/dist/server/v1/changeName.js.map +1 -0
  74. package/dist/server/v1/checkAuthSub.js +58 -0
  75. package/dist/server/v1/checkAuthSub.js.map +1 -0
  76. package/dist/server/v1/dashboardLink.js +30 -0
  77. package/dist/server/v1/dashboardLink.js.map +1 -0
  78. package/dist/server/v1/getSuspendedSubs.js +76 -0
  79. package/dist/server/v1/getSuspendedSubs.js.map +1 -0
  80. package/dist/server/v1/index.js +28 -0
  81. package/dist/server/v1/index.js.map +1 -0
  82. package/dist/server/v1/manualCreateSub.js +159 -0
  83. package/dist/server/v1/manualCreateSub.js.map +1 -0
  84. package/dist/server/v1/refund.js +78 -0
  85. package/dist/server/v1/refund.js.map +1 -0
  86. package/dist/server/v1/retrySubscription.js +81 -0
  87. package/dist/server/v1/retrySubscription.js.map +1 -0
  88. package/dist/server/v1/searchUser.js +169 -0
  89. package/dist/server/v1/searchUser.js.map +1 -0
  90. package/dist/server/v1/slack.js +14 -0
  91. package/dist/server/v1/slack.js.map +1 -0
  92. package/dist/src/index.js +24 -0
  93. package/dist/src/index.js.map +1 -0
  94. package/dist/src/pk-client.js +1103 -0
  95. package/dist/src/pk-client.js.map +1 -0
  96. package/dist/src/pk-sub.js +310 -0
  97. package/dist/src/pk-sub.js.map +1 -0
  98. package/dist/src/slack-client.js +48 -0
  99. package/dist/src/slack-client.js.map +1 -0
  100. package/dist/src/types.js +3 -0
  101. package/dist/src/types.js.map +1 -0
  102. package/dist/support/index.js +19 -0
  103. package/dist/support/index.js.map +1 -0
  104. package/dist/support/metrics.js +21 -0
  105. package/dist/support/metrics.js.map +1 -0
  106. package/dist/support/mixpanel.js +7 -0
  107. package/dist/support/mixpanel.js.map +1 -0
  108. package/dist/support/slack-client.js +51 -0
  109. package/dist/support/slack-client.js.map +1 -0
  110. package/dist/support/uploadcare.js +140 -0
  111. package/dist/support/uploadcare.js.map +1 -0
  112. package/dist/support/utils.js +69 -0
  113. package/dist/support/utils.js.map +1 -0
  114. package/dist/typed-model/asset-category.js +38 -0
  115. package/dist/typed-model/asset-category.js.map +1 -0
  116. package/dist/typed-model/asset-constent-person.js +56 -0
  117. package/dist/typed-model/asset-constent-person.js.map +1 -0
  118. package/dist/typed-model/blocked-ip.js +34 -0
  119. package/dist/typed-model/blocked-ip.js.map +1 -0
  120. package/dist/typed-model/category-tag.js +46 -0
  121. package/dist/typed-model/category-tag.js.map +1 -0
  122. package/dist/typed-model/consent-person.js +55 -0
  123. package/dist/typed-model/consent-person.js.map +1 -0
  124. package/dist/typed-model/content-category-tag.js +58 -0
  125. package/dist/typed-model/content-category-tag.js.map +1 -0
  126. package/dist/typed-model/content-like.js +58 -0
  127. package/dist/typed-model/content-like.js.map +1 -0
  128. package/dist/typed-model/credit-payment.js +389 -0
  129. package/dist/typed-model/credit-payment.js.map +1 -0
  130. package/dist/typed-model/credit-payout.js +42 -0
  131. package/dist/typed-model/credit-payout.js.map +1 -0
  132. package/dist/typed-model/credit-purchase.js +46 -0
  133. package/dist/typed-model/credit-purchase.js.map +1 -0
  134. package/dist/typed-model/customer-profile.js +51 -0
  135. package/dist/typed-model/customer-profile.js.map +1 -0
  136. package/dist/typed-model/dashboard-user-permissions.js +34 -0
  137. package/dist/typed-model/dashboard-user-permissions.js.map +1 -0
  138. package/dist/typed-model/dashboard-user.js +109 -0
  139. package/dist/typed-model/dashboard-user.js.map +1 -0
  140. package/dist/typed-model/fan-list.js +46 -0
  141. package/dist/typed-model/fan-list.js.map +1 -0
  142. package/dist/typed-model/fraud.js +41 -0
  143. package/dist/typed-model/fraud.js.map +1 -0
  144. package/dist/typed-model/index.js +101 -0
  145. package/dist/typed-model/index.js.map +1 -0
  146. package/dist/typed-model/list-member.js +47 -0
  147. package/dist/typed-model/list-member.js.map +1 -0
  148. package/dist/typed-model/mass-message.js +43 -0
  149. package/dist/typed-model/mass-message.js.map +1 -0
  150. package/dist/typed-model/media-asset.js +73 -0
  151. package/dist/typed-model/media-asset.js.map +1 -0
  152. package/dist/typed-model/media-category.js +49 -0
  153. package/dist/typed-model/media-category.js.map +1 -0
  154. package/dist/typed-model/media.js +59 -0
  155. package/dist/typed-model/media.js.map +1 -0
  156. package/dist/typed-model/message-media.js +53 -0
  157. package/dist/typed-model/message-media.js.map +1 -0
  158. package/dist/typed-model/message.js +191 -0
  159. package/dist/typed-model/message.js.map +1 -0
  160. package/dist/typed-model/notification.js +84 -0
  161. package/dist/typed-model/notification.js.map +1 -0
  162. package/dist/typed-model/pb-sequelize.js +25 -0
  163. package/dist/typed-model/pb-sequelize.js.map +1 -0
  164. package/dist/typed-model/phone-chat-participation.js +127 -0
  165. package/dist/typed-model/phone-chat-participation.js.map +1 -0
  166. package/dist/typed-model/phone-chat.js +118 -0
  167. package/dist/typed-model/phone-chat.js.map +1 -0
  168. package/dist/typed-model/phone-owner.js +266 -0
  169. package/dist/typed-model/phone-owner.js.map +1 -0
  170. package/dist/typed-model/phone-user.js +109 -0
  171. package/dist/typed-model/phone-user.js.map +1 -0
  172. package/dist/typed-model/premium-content.js +123 -0
  173. package/dist/typed-model/premium-content.js.map +1 -0
  174. package/dist/typed-model/purchased-content.js +69 -0
  175. package/dist/typed-model/purchased-content.js.map +1 -0
  176. package/dist/typed-model/short-url.js +88 -0
  177. package/dist/typed-model/short-url.js.map +1 -0
  178. package/dist/typed-model/subscription-bundle.js +53 -0
  179. package/dist/typed-model/subscription-bundle.js.map +1 -0
  180. package/dist/typed-model/subscription-category-tags.js +58 -0
  181. package/dist/typed-model/subscription-category-tags.js.map +1 -0
  182. package/dist/typed-model/subscription-plan.js +126 -0
  183. package/dist/typed-model/subscription-plan.js.map +1 -0
  184. package/dist/typed-model/subscription-promotion.js +93 -0
  185. package/dist/typed-model/subscription-promotion.js.map +1 -0
  186. package/dist/typed-model/subscription.js +123 -0
  187. package/dist/typed-model/subscription.js.map +1 -0
  188. package/dist/typed-model/twilio-phone.js +92 -0
  189. package/dist/typed-model/twilio-phone.js.map +1 -0
  190. package/dist/typed-model/user-follow.js +103 -0
  191. package/dist/typed-model/user-follow.js.map +1 -0
  192. package/dist/typed-model/verification-code.js +52 -0
  193. package/dist/typed-model/verification-code.js.map +1 -0
  194. package/index.ts +3 -0
  195. package/package.json +72 -0
  196. package/src/index.ts +2 -0
  197. package/src/pk-client.ts +1650 -0
  198. package/src/pk-sub.ts +455 -0
  199. package/src/types.ts +40 -0
  200. package/support/index.ts +2 -0
  201. package/support/slack-client.ts +43 -0
  202. package/support/uploadcare.ts +191 -0
  203. package/support/utils.ts +72 -0
  204. package/typed-model/asset-category.ts +57 -0
  205. package/typed-model/asset-constent-person.ts +82 -0
  206. package/typed-model/blocked-ip.ts +52 -0
  207. package/typed-model/category-tag.ts +66 -0
  208. package/typed-model/consent-person.ts +78 -0
  209. package/typed-model/content-category-tag.ts +79 -0
  210. package/typed-model/content-like.ts +79 -0
  211. package/typed-model/credit-payment.ts +678 -0
  212. package/typed-model/credit-payout.ts +62 -0
  213. package/typed-model/credit-purchase.ts +67 -0
  214. package/typed-model/customer-profile.ts +70 -0
  215. package/typed-model/dashboard-user-permissions.ts +51 -0
  216. package/typed-model/dashboard-user.ts +146 -0
  217. package/typed-model/fan-list.ts +68 -0
  218. package/typed-model/fraud.ts +62 -0
  219. package/typed-model/index.ts +58 -0
  220. package/typed-model/list-member.ts +67 -0
  221. package/typed-model/mass-message.ts +64 -0
  222. package/typed-model/media-asset.ts +99 -0
  223. package/typed-model/media-category.ts +70 -0
  224. package/typed-model/media.ts +85 -0
  225. package/typed-model/message-media.ts +74 -0
  226. package/typed-model/message.ts +258 -0
  227. package/typed-model/notification.ts +130 -0
  228. package/typed-model/pb-sequelize.ts +25 -0
  229. package/typed-model/phone-chat-participation.ts +166 -0
  230. package/typed-model/phone-chat.ts +185 -0
  231. package/typed-model/phone-owner.ts +341 -0
  232. package/typed-model/phone-user.ts +146 -0
  233. package/typed-model/premium-content.ts +164 -0
  234. package/typed-model/purchased-content.ts +100 -0
  235. package/typed-model/short-url.ts +120 -0
  236. package/typed-model/subscription-bundle.ts +75 -0
  237. package/typed-model/subscription-category-tags.ts +79 -0
  238. package/typed-model/subscription-plan.ts +171 -0
  239. package/typed-model/subscription-promotion.ts +125 -0
  240. package/typed-model/subscription.ts +170 -0
  241. package/typed-model/twilio-phone.ts +149 -0
  242. package/typed-model/user-follow.ts +141 -0
  243. package/typed-model/verification-code.ts +74 -0
@@ -0,0 +1,170 @@
1
+ import {
2
+ Model,
3
+ InferAttributes,
4
+ InferCreationAttributes,
5
+ CreationOptional,
6
+ DataTypes,
7
+ fn,
8
+ NOW,
9
+ } from "sequelize";
10
+ import { ProcessorId } from "./customer-profile";
11
+
12
+ import sequelize from "./pb-sequelize";
13
+ import PhoneOwner from "./phone-owner";
14
+ import PhoneUser from "./phone-user";
15
+ import SubscriptionPromotion from "./subscription-promotion";
16
+
17
+ // order of InferAttributes & InferCreationAttributes is important.
18
+ class Subscription extends Model<
19
+ InferAttributes<Subscription>,
20
+ InferCreationAttributes<Subscription>
21
+ > {
22
+ // 'CreationOptional' is a special type that marks the field as optional
23
+ // when creating an instance of the model (such as using Model.create()).
24
+ declare id: CreationOptional<string>;
25
+ declare userId: string;
26
+ declare subscriptionPlanId: string;
27
+ declare processorId: ProcessorId;
28
+ declare paypalSubscriptionId?: string;
29
+ declare paypalOrderId?: string;
30
+ declare paypalBillingToken?: string;
31
+ declare lastPaymentDate?: Date;
32
+ declare cancelDate?: Date;
33
+ declare pkSubscriptionId?: string;
34
+ declare numberOfPayments?: number;
35
+ declare subscriptionPromotionId?: string;
36
+ declare stale: CreationOptional<boolean>;
37
+ declare createdAt: CreationOptional<Date>;
38
+ declare updatedAt: CreationOptional<Date>;
39
+
40
+ static getById: (id: string) => Promise<Subscription | null>;
41
+ static getByUserId: (userId: string) => Promise<Subscription | null>;
42
+ static getBySubscriptionPlanId: (
43
+ subscriptionPlanId: string
44
+ ) => Promise<Subscription | null>;
45
+ static getByPaypalSubscriptionId: (
46
+ paypalSubscriptionId: string
47
+ ) => Promise<Subscription | null>;
48
+ }
49
+
50
+ Subscription.init(
51
+ {
52
+ id: {
53
+ type: DataTypes.BIGINT,
54
+ primaryKey: true,
55
+ allowNull: false,
56
+ defaultValue: sequelize.fn("next_id"),
57
+ },
58
+ userId: {
59
+ type: DataTypes.BIGINT,
60
+ allowNull: false,
61
+ field: "user_id",
62
+ },
63
+ subscriptionPlanId: {
64
+ type: DataTypes.BIGINT,
65
+ allowNull: false,
66
+ field: "subscription_plan_id",
67
+ },
68
+ paypalSubscriptionId: {
69
+ type: DataTypes.STRING,
70
+ field: "paypal_subscription_id",
71
+ },
72
+ paypalOrderId: {
73
+ type: DataTypes.STRING,
74
+ field: "paypal_order_id",
75
+ },
76
+ paypalBillingToken: {
77
+ type: DataTypes.STRING,
78
+ field: "paypal_billing_token",
79
+ },
80
+ lastPaymentDate: {
81
+ type: DataTypes.DATE,
82
+ field: "last_payment_date",
83
+ },
84
+ cancelDate: {
85
+ type: DataTypes.DATE,
86
+ field: "cancel_date",
87
+ },
88
+ pkSubscriptionId: {
89
+ type: DataTypes.STRING,
90
+ field: "pk_subscription_id",
91
+ },
92
+ numberOfPayments: {
93
+ type: DataTypes.INTEGER,
94
+ defaultValue: 0,
95
+ field: "number_of_payments",
96
+ },
97
+ subscriptionPromotionId: {
98
+ type: DataTypes.BIGINT,
99
+ field: "subscription_promotion_id",
100
+ },
101
+ processorId: {
102
+ type: DataTypes.STRING,
103
+ field: "processor_id",
104
+ defaultValue: ProcessorId.HUMBOLDT,
105
+ },
106
+ stale: {
107
+ type: DataTypes.BOOLEAN,
108
+ field: "stale",
109
+ defaultValue: false,
110
+ },
111
+ createdAt: DataTypes.DATE,
112
+ updatedAt: DataTypes.DATE,
113
+ },
114
+ {
115
+ tableName: "subscriptions",
116
+ sequelize,
117
+ }
118
+ );
119
+
120
+ /*
121
+ ====================================================================
122
+ Class functions
123
+ ====================================================================
124
+ */
125
+
126
+ Subscription.getById = async (id: string) => {
127
+ return await Subscription.findOne({
128
+ where: {
129
+ id,
130
+ },
131
+ });
132
+ };
133
+
134
+ Subscription.getByUserId = async (userId: string) => {
135
+ return await Subscription.findOne({
136
+ where: {
137
+ userId,
138
+ },
139
+ });
140
+ };
141
+
142
+ Subscription.getBySubscriptionPlanId = async (subscriptionPlanId: string) => {
143
+ return await Subscription.findOne({
144
+ where: {
145
+ subscriptionPlanId,
146
+ },
147
+ });
148
+ };
149
+
150
+ Subscription.getByPaypalSubscriptionId = async (
151
+ paypalSubscriptionId: string
152
+ ) => {
153
+ return await Subscription.findOne({
154
+ where: {
155
+ paypalSubscriptionId,
156
+ },
157
+ });
158
+ };
159
+
160
+ Subscription.hasOne(SubscriptionPromotion, {
161
+ foreignKey: "id",
162
+ sourceKey: "subscriptionPromotionId",
163
+ });
164
+ SubscriptionPromotion.hasMany(Subscription, {
165
+ foreignKey: "subscription_plan_id",
166
+ });
167
+ Subscription.hasOne(PhoneUser, { foreignKey: "id", sourceKey: "userId" });
168
+ PhoneUser.hasMany(Subscription, { foreignKey: "user_id", sourceKey: "id" });
169
+
170
+ export default Subscription;
@@ -0,0 +1,149 @@
1
+ import {
2
+ Model,
3
+ InferAttributes,
4
+ InferCreationAttributes,
5
+ CreationOptional,
6
+ DataTypes,
7
+ fn,
8
+ } from "sequelize";
9
+
10
+ import sequelize from "./pb-sequelize";
11
+ import client = require("twilio");
12
+ const accountSid = process.env.TWILIO_ACCOUNT_SID;
13
+ const authToken = process.env.TWILIO_AUTH_TOKEN;
14
+ let twilioClient = client(accountSid, authToken);
15
+
16
+ // order of InferAttributes & InferCreationAttributes is important.
17
+ class TwilioPhone extends Model<
18
+ InferAttributes<TwilioPhone>,
19
+ InferCreationAttributes<TwilioPhone>
20
+ > {
21
+ // 'CreationOptional' is a special type that marks the field as optional
22
+ // when creating an instance of the model (such as using Model.create()).
23
+ declare id: CreationOptional<string>;
24
+ declare phone: string;
25
+ declare type: string;
26
+ declare createdAt: CreationOptional<Date>;
27
+ declare updatedAt: CreationOptional<Date>;
28
+ static getByPhone: (phone: string) => Promise<TwilioPhone | null>;
29
+ static getById: (id: string) => Promise<TwilioPhone | null>;
30
+ static getOrCreateByPhone: (phone: string) => Promise<any>;
31
+ static getAvailableChatPhone: (
32
+ userId: string,
33
+ ownerId: string
34
+ ) => Promise<TwilioPhone | null>;
35
+ static sendMessageFromNumberToPhone: (
36
+ fromNumber: string,
37
+ phone: string,
38
+ message: string,
39
+ media?: string
40
+ ) => Promise<any>;
41
+ static sendMessageToPhone: (
42
+ twilioPhone: { phone: string },
43
+ phone: string,
44
+ message: string,
45
+ media?: string
46
+ ) => Promise<any>;
47
+ }
48
+
49
+ TwilioPhone.init(
50
+ {
51
+ id: {
52
+ type: DataTypes.BIGINT,
53
+ primaryKey: true,
54
+ allowNull: false,
55
+ defaultValue: sequelize.fn("next_id"),
56
+ },
57
+ phone: {
58
+ type: DataTypes.STRING,
59
+ allowNull: false,
60
+ field: "phone",
61
+ unique: true,
62
+ },
63
+ type: {
64
+ type: DataTypes.STRING,
65
+ },
66
+ createdAt: DataTypes.DATE,
67
+ updatedAt: DataTypes.DATE,
68
+ },
69
+ {
70
+ tableName: "twilio_phones",
71
+ sequelize,
72
+ }
73
+ );
74
+
75
+ /*
76
+ ====================================================================
77
+ Class functions
78
+ ====================================================================
79
+ */
80
+
81
+ TwilioPhone.getByPhone = async (phone: string) => {
82
+ return await TwilioPhone.findOne({
83
+ where: {
84
+ phone,
85
+ },
86
+ });
87
+ };
88
+
89
+ TwilioPhone.getById = async (id: string) => {
90
+ return await TwilioPhone.findOne({
91
+ where: {
92
+ id,
93
+ },
94
+ });
95
+ };
96
+
97
+ TwilioPhone.getOrCreateByPhone = async (phone: string) => {
98
+ let phoneObject = await TwilioPhone.getByPhone(phone);
99
+
100
+ if (phoneObject) {
101
+ return phoneObject;
102
+ }
103
+
104
+ throw new Error("This phone does not exist");
105
+ };
106
+
107
+ TwilioPhone.getAvailableChatPhone = async (userId: string, ownerId: string) => {
108
+ let phone = await TwilioPhone.findOne({
109
+ where: sequelize.literal(`
110
+ type='chat' AND id NOT IN
111
+ (SELECT tp.id FROM twilio_phones tp
112
+ JOIN chats ch on ch.twilio_phone_id = tp.id
113
+ JOIN chat_participants cps on cps.chat_id = ch.id
114
+ WHERE type = 'chat' AND (ch.owner_id = ${ownerId} OR cps.user_id = ${userId}))
115
+ `),
116
+ });
117
+
118
+ return phone;
119
+ };
120
+
121
+ TwilioPhone.sendMessageFromNumberToPhone = async (
122
+ fromNumber: string,
123
+ phone: string,
124
+ message: string,
125
+ media?: string
126
+ ) => {
127
+ return await twilioClient.messages.create({
128
+ body: message || "",
129
+ from: fromNumber,
130
+ to: phone,
131
+ mediaUrl: media,
132
+ });
133
+ };
134
+
135
+ TwilioPhone.sendMessageToPhone = async (
136
+ twilioPhone: { phone: string },
137
+ phone: string,
138
+ message: string,
139
+ media?: string
140
+ ) => {
141
+ return await twilioClient.messages.create({
142
+ body: message || "",
143
+ from: twilioPhone.phone,
144
+ to: phone,
145
+ mediaUrl: media,
146
+ });
147
+ };
148
+
149
+ export default TwilioPhone;
@@ -0,0 +1,141 @@
1
+ import {
2
+ Model,
3
+ InferAttributes,
4
+ InferCreationAttributes,
5
+ CreationOptional,
6
+ DataTypes,
7
+ fn,
8
+ } from "sequelize";
9
+
10
+ import sequelize from "./pb-sequelize";
11
+ import Notification from "./notification";
12
+ import PhoneUser from "./phone-user";
13
+ import PhoneOwner from "./phone-owner";
14
+
15
+ // order of InferAttributes & InferCreationAttributes is important.
16
+ class UserFollow extends Model<
17
+ InferAttributes<UserFollow>,
18
+ InferCreationAttributes<UserFollow>
19
+ > {
20
+ // 'CreationOptional' is a special type that marks the field as optional
21
+ // when creating an instance of the model (such as using Model.create()).
22
+ declare id: CreationOptional<string>;
23
+ declare userId: string;
24
+ declare ownerId: string;
25
+ declare active?: boolean;
26
+ declare createdAt: CreationOptional<Date>;
27
+ declare updatedAt: CreationOptional<Date>;
28
+ static getAllByUserId: (
29
+ userId: string,
30
+ ownerId: string
31
+ ) => Promise<UserFollow[]>;
32
+ static getByUserIdAndOwnerId: (
33
+ userId: string,
34
+ ownerId: string
35
+ ) => Promise<UserFollow | null>;
36
+ static createByUserIdAndOwnerId: (
37
+ userId: string,
38
+ ownerId: string
39
+ ) => Promise<any>;
40
+ }
41
+
42
+ UserFollow.init(
43
+ {
44
+ id: {
45
+ type: DataTypes.BIGINT,
46
+ primaryKey: true,
47
+ allowNull: false,
48
+ defaultValue: sequelize.fn("next_id"),
49
+ },
50
+ userId: {
51
+ type: DataTypes.BIGINT,
52
+ field: "user_id",
53
+ },
54
+ ownerId: {
55
+ type: DataTypes.BIGINT,
56
+ field: "owner_id",
57
+ },
58
+ active: {
59
+ type: DataTypes.BOOLEAN,
60
+ field: "active",
61
+ defaultValue: true,
62
+ },
63
+ createdAt: DataTypes.DATE,
64
+ updatedAt: DataTypes.DATE,
65
+ },
66
+ {
67
+ tableName: "user_follows",
68
+ sequelize,
69
+ }
70
+ );
71
+
72
+ /*
73
+ ====================================================================
74
+ Class functions
75
+ ====================================================================
76
+ */
77
+
78
+ UserFollow.getAllByUserId = async (userId: string, ownerId: string) => {
79
+ return await UserFollow.findAll({
80
+ where: {
81
+ userId,
82
+ active: true,
83
+ },
84
+ });
85
+ };
86
+
87
+ UserFollow.getByUserIdAndOwnerId = async (userId: string, ownerId: string) => {
88
+ return await UserFollow.findOne({
89
+ where: {
90
+ userId,
91
+ ownerId,
92
+ },
93
+ });
94
+ };
95
+
96
+ UserFollow.createByUserIdAndOwnerId = async (
97
+ userId: string,
98
+ ownerId: string
99
+ ) => {
100
+ let follow = await UserFollow.getByUserIdAndOwnerId(userId, ownerId);
101
+ if (!follow) {
102
+ follow = await UserFollow.create({ userId, ownerId });
103
+ try {
104
+ await Notification.create({
105
+ ownerId,
106
+ userId,
107
+ userFollowId: follow.id,
108
+ type: "follow",
109
+ });
110
+ } catch (err) {
111
+ console.log(err);
112
+ }
113
+ }
114
+ follow.active = true;
115
+ await follow.save();
116
+ return follow;
117
+ };
118
+
119
+ PhoneUser.hasMany(UserFollow, {
120
+ foreignKey: "user_id",
121
+ onDelete: "NO ACTION",
122
+ constraints: false,
123
+ });
124
+ PhoneOwner.hasMany(UserFollow, {
125
+ foreignKey: "owner_id",
126
+ onDelete: "NO ACTION",
127
+ constraints: false,
128
+ });
129
+ UserFollow.hasOne(PhoneUser, {
130
+ sourceKey: "user_id",
131
+ foreignKey: "id",
132
+ onDelete: "NO ACTION",
133
+ constraints: false,
134
+ });
135
+ Notification.hasOne(UserFollow, {
136
+ foreignKey: "id",
137
+ sourceKey: "userFollowId",
138
+ constraints: false,
139
+ });
140
+
141
+ export default UserFollow;
@@ -0,0 +1,74 @@
1
+ import {
2
+ Model,
3
+ InferAttributes,
4
+ InferCreationAttributes,
5
+ CreationOptional,
6
+ DataTypes,
7
+ fn,
8
+ } from "sequelize";
9
+
10
+ import sequelize from "./pb-sequelize";
11
+
12
+ // order of InferAttributes & InferCreationAttributes is important.
13
+ class VerificationCode extends Model<
14
+ InferAttributes<VerificationCode>,
15
+ InferCreationAttributes<VerificationCode>
16
+ > {
17
+ // 'CreationOptional' is a special type that marks the field as optional
18
+ // when creating an instance of the model (such as using Model.create()).
19
+ declare id: CreationOptional<string>;
20
+ declare userId: string;
21
+ declare code: string;
22
+ declare verified?: boolean;
23
+ declare createdAt: CreationOptional<Date>;
24
+ declare updatedAt: CreationOptional<Date>;
25
+ static createCode: (userId: string) => Promise<VerificationCode>;
26
+ }
27
+
28
+ VerificationCode.init(
29
+ {
30
+ id: {
31
+ type: DataTypes.BIGINT,
32
+ primaryKey: true,
33
+ allowNull: false,
34
+ defaultValue: sequelize.fn("next_id"),
35
+ },
36
+ userId: {
37
+ type: DataTypes.BIGINT,
38
+ allowNull: false,
39
+ field: "user_id",
40
+ },
41
+ code: {
42
+ type: DataTypes.STRING,
43
+ field: "code",
44
+ },
45
+ verified: {
46
+ type: DataTypes.BOOLEAN,
47
+ field: "verified",
48
+ defaultValue: false,
49
+ },
50
+ createdAt: DataTypes.DATE,
51
+ updatedAt: DataTypes.DATE,
52
+ },
53
+ {
54
+ // Other model options go here
55
+ tableName: "verification_codes",
56
+ sequelize,
57
+ }
58
+ );
59
+
60
+ /*
61
+ ====================================================================
62
+ Class functions
63
+ ====================================================================
64
+ */
65
+
66
+ VerificationCode.createCode = async (userId: string) => {
67
+ let code = Math.floor(100000 + Math.random() * 900000) + "";
68
+ return await VerificationCode.create({
69
+ userId,
70
+ code,
71
+ });
72
+ };
73
+
74
+ export default VerificationCode;