@kipicore/dbcore 1.1.473 → 1.1.474

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.
@@ -536,7 +536,9 @@ export declare enum PARENTS_CHILDREN_CURRENT {
536
536
  export declare enum USER_PAYOUT_TYPE {
537
537
  PERCENTAGE = "PERCENTAGE",
538
538
  HOURLY = "HOURLY",
539
- MONTHLY = "MONTHLY"
539
+ MONTHLY = "MONTHLY",
540
+ ANNUAL = "ANNUAL",
541
+ NONE = "NONE"
540
542
  }
541
543
  export declare enum QDC_NAME {
542
544
  LAKSHMI_VIKAS_SANKUL_ANR02 = "LAKSHMI_VIKAS_SANKUL_ANR02"
@@ -649,6 +649,8 @@ var USER_PAYOUT_TYPE;
649
649
  USER_PAYOUT_TYPE["PERCENTAGE"] = "PERCENTAGE";
650
650
  USER_PAYOUT_TYPE["HOURLY"] = "HOURLY";
651
651
  USER_PAYOUT_TYPE["MONTHLY"] = "MONTHLY";
652
+ USER_PAYOUT_TYPE["ANNUAL"] = "ANNUAL";
653
+ USER_PAYOUT_TYPE["NONE"] = "NONE";
652
654
  })(USER_PAYOUT_TYPE || (exports.USER_PAYOUT_TYPE = USER_PAYOUT_TYPE = {}));
653
655
  var QDC_NAME;
654
656
  (function (QDC_NAME) {
@@ -0,0 +1,2 @@
1
+ export function up(queryInterface: any, Sequelize: any): Promise<void>;
2
+ export function down(queryInterface: any, Sequelize: any): Promise<void>;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ module.exports = {
3
+ up: async (queryInterface, Sequelize) => {
4
+ const transaction = await queryInterface.sequelize.transaction();
5
+ try {
6
+ await queryInterface.changeColumn('user_payouts', 'type', {
7
+ type: Sequelize.STRING,
8
+ allowNull: false,
9
+ defaultValue: 'NONE',
10
+ }, { transaction });
11
+ await transaction.commit();
12
+ }
13
+ catch (error) {
14
+ await transaction.rollback();
15
+ throw error;
16
+ }
17
+ },
18
+ down: async (queryInterface, Sequelize) => {
19
+ const transaction = await queryInterface.sequelize.transaction();
20
+ try {
21
+ await queryInterface.changeColumn('user_payouts', 'type', {
22
+ type: Sequelize.ENUM('PERCENTAGE', 'HOURLY', 'MONTHLY', 'ANNUAL', 'NONE'),
23
+ allowNull: false,
24
+ }, { transaction });
25
+ await transaction.commit();
26
+ }
27
+ catch (error) {
28
+ await transaction.rollback();
29
+ throw error;
30
+ }
31
+ },
32
+ };
@@ -0,0 +1,2 @@
1
+ export function up(queryInterface: any, Sequelize: any): Promise<void>;
2
+ export function down(queryInterface: any, Sequelize: any): Promise<void>;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ module.exports = {
3
+ up: async (queryInterface, Sequelize) => {
4
+ const transaction = await queryInterface.sequelize.transaction();
5
+ try {
6
+ await queryInterface.changeColumn('user_payout_details', 'type', {
7
+ type: Sequelize.STRING,
8
+ allowNull: false,
9
+ defaultValue: 'NONE',
10
+ }, { transaction });
11
+ await transaction.commit();
12
+ }
13
+ catch (error) {
14
+ await transaction.rollback();
15
+ throw error;
16
+ }
17
+ },
18
+ down: async (queryInterface, Sequelize) => {
19
+ const transaction = await queryInterface.sequelize.transaction();
20
+ try {
21
+ await queryInterface.changeColumn('user_payout_details', 'type', {
22
+ type: Sequelize.ENUM('PERCENTAGE', 'HOURLY', 'MONTHLY', 'ANNUAL', 'NONE'),
23
+ allowNull: false,
24
+ }, { transaction });
25
+ await transaction.commit();
26
+ }
27
+ catch (error) {
28
+ await transaction.rollback();
29
+ throw error;
30
+ }
31
+ },
32
+ };
@@ -68,7 +68,7 @@ UserPayoutDetailsModel.init({
68
68
  payableAmount: { type: sequelize_1.DataTypes.FLOAT, allowNull: true, defaultValue: 0, field: 'payable_amount' },
69
69
  amountPerHour: { type: sequelize_1.DataTypes.FLOAT, allowNull: true, defaultValue: 0, field: 'amount_per_hour' },
70
70
  percentage: { type: sequelize_1.DataTypes.FLOAT, allowNull: true, defaultValue: 0, field: 'percentage' },
71
- type: { type: sequelize_1.DataTypes.ENUM, values: Object.values(app_1.USER_PAYOUT_TYPE), allowNull: false },
71
+ type: { type: sequelize_1.DataTypes.STRING, allowNull: false, defaultValue: app_1.USER_PAYOUT_TYPE.NONE },
72
72
  userId: { type: sequelize_1.DataTypes.UUID, allowNull: false, field: 'user_id' },
73
73
  instituteId: { type: sequelize_1.DataTypes.UUID, allowNull: false, field: 'institute_id' },
74
74
  }, {
@@ -17,6 +17,5 @@ declare class UserPayoutModel extends Model<IUserPayoutModelAttributes, TUserPay
17
17
  readonly deletedAt: Date;
18
18
  readonly updatedAt: Date;
19
19
  static associate(models: any): void;
20
- static addHooks(models: any): void;
21
20
  }
22
21
  export default UserPayoutModel;
@@ -4,7 +4,6 @@ const sequelize_1 = require("sequelize");
4
4
  const index_1 = require("./index");
5
5
  const app_1 = require("../../constants/app");
6
6
  const date_fns_1 = require("date-fns");
7
- const utils_1 = require("../../helpers/utils");
8
7
  class UserPayoutModel extends sequelize_1.Model {
9
8
  static associate(models) {
10
9
  const { UserModel, InstituteModel } = models;
@@ -36,181 +35,6 @@ class UserPayoutModel extends sequelize_1.Model {
36
35
  foreignKey: { name: 'userId' },
37
36
  as: 'userHasPayoutList',
38
37
  });
39
- // UserPayoutModel.hasMany(UserPayoutDetailsModel, {
40
- // foreignKey: { name: 'userPayoutId' },
41
- // as: 'userPayoutDetailsList',
42
- // });
43
- }
44
- static addHooks(models) {
45
- const { UserPayoutDetailsModel, SubjectHasPayFeeHistory, LectureModel, AttendanceModel } = models;
46
- const afterCreateOrUpdateHook = async (userPayout, user = null, transaction, filterOptions = {}) => {
47
- try {
48
- if (!userPayout.userPayoutDetailsList || !userPayout.userPayoutDetailsList.length)
49
- return userPayout;
50
- userPayout.userPayoutDetailsList = (0, utils_1.getUniqueArrayByFields)(userPayout.userPayoutDetailsList, ['subjectId']);
51
- const options = { transaction };
52
- if (user)
53
- options.userId = user.id;
54
- const now = new Date();
55
- const previousMonthDate = (0, date_fns_1.subMonths)(now, 1);
56
- let startOfPreviousMonth = (0, date_fns_1.startOfMonth)(previousMonthDate).toISOString();
57
- let endOfPreviousMonth = (0, date_fns_1.endOfMonth)(previousMonthDate).toISOString();
58
- if (filterOptions.include) {
59
- filterOptions.include = !Array.isArray(filterOptions.include) ? [filterOptions.include] : filterOptions.include;
60
- const historyListInclude = filterOptions.include.find((include) => include.as === 'userPayoutHistoryList');
61
- let where = {};
62
- if (historyListInclude && historyListInclude.where && historyListInclude.where[sequelize_1.Op.and]) {
63
- where = historyListInclude.where[sequelize_1.Op.and].pop();
64
- }
65
- if (where.payout_start_time && where.payout_end_time) {
66
- startOfPreviousMonth = where.payout_start_time[sequelize_1.Op.gte];
67
- endOfPreviousMonth = where.payout_end_time[sequelize_1.Op.lte];
68
- }
69
- }
70
- const attendanceWhereAnd = [{ date: { $gte: startOfPreviousMonth } }, { date: { $lte: endOfPreviousMonth } }];
71
- const existUserPayout = (await UserPayoutModel.findOne({
72
- where: { id: userPayout.id },
73
- include: [{ association: 'userPayoutDetailsList' }],
74
- transaction,
75
- }));
76
- if (!existUserPayout)
77
- return userPayout;
78
- if (userPayout.type === app_1.USER_PAYOUT_TYPE.PERCENTAGE) {
79
- let feeTotalBySubjectList = await SubjectHasPayFeeHistory.findAll({
80
- where: {
81
- instituteId: existUserPayout.instituteId,
82
- subjectId: {
83
- [sequelize_1.Op.in]: userPayout.userPayoutDetailsList.map(detail => detail.subjectId),
84
- },
85
- },
86
- attributes: [
87
- ['subject_id', 'subjectId'],
88
- [sequelize_1.Sequelize.fn('sum', sequelize_1.Sequelize.col('amount')), 'amount'],
89
- ],
90
- group: ['subject_id', 'created_at'],
91
- raw: true,
92
- });
93
- feeTotalBySubjectList = JSON.parse(JSON.stringify(feeTotalBySubjectList));
94
- userPayout.userPayoutDetailsList = userPayout.userPayoutDetailsList.map(detail => {
95
- detail.payableAmount = feeTotalBySubjectList
96
- .filter((fee) => fee.subjectId === detail.subjectId)
97
- .reduce((sum, record) => {
98
- if (detail.percentage) {
99
- const amount = ((record.amount || 0) * detail.percentage) / 100;
100
- return sum + parseFloat(amount.toFixed(2));
101
- }
102
- return sum;
103
- }, 0);
104
- return detail;
105
- });
106
- }
107
- if (userPayout.type === app_1.USER_PAYOUT_TYPE.HOURLY) {
108
- let lectureList = (await LectureModel.findAll({
109
- where: {
110
- instituteId: existUserPayout.instituteId,
111
- subject: {
112
- [sequelize_1.Op.in]: userPayout.userPayoutDetailsList.map(detail => detail.subjectId),
113
- },
114
- [sequelize_1.Op.or]: [{ primaryUserId: existUserPayout.userId }, { secondaryUserId: existUserPayout.userId }],
115
- },
116
- include: [{ association: 'workingDay' }],
117
- }));
118
- lectureList = JSON.parse(JSON.stringify(lectureList));
119
- const currentMonthAttendanceList = await AttendanceModel.find({
120
- $and: attendanceWhereAnd,
121
- userType: userPayout.userType,
122
- 'users.userId': userPayout.userId,
123
- 'users.status': { $ne: app_1.ATTENDANCE_STATUS.ABSENT },
124
- });
125
- userPayout.userPayoutDetailsList = userPayout.userPayoutDetailsList.map(detail => {
126
- let totalMinutes = 0;
127
- currentMonthAttendanceList.forEach((attendance) => {
128
- const dayName = (0, date_fns_1.format)(attendance.date, 'EEEE');
129
- const lecturesByDay = lectureList.filter(lecture => lecture.workingDay.dayName === dayName && lecture.subject === detail.subjectId);
130
- if (lecturesByDay.length) {
131
- totalMinutes += lecturesByDay.reduce((sum, record) => sum + record.slotDuration, 0);
132
- }
133
- });
134
- detail.payableAmount = ((detail.amountPerHour || 0) / app_1.SIXTY) * totalMinutes;
135
- return detail;
136
- });
137
- }
138
- const userPayoutDetailsList = [];
139
- for (let detail of userPayout.userPayoutDetailsList) {
140
- detail.userPayoutId = existUserPayout.id;
141
- detail.userId = existUserPayout.userId;
142
- detail.instituteId = existUserPayout.instituteId;
143
- detail.type = existUserPayout.type;
144
- const found = existUserPayout?.userPayoutDetailsList?.find(existing => detail.userPayoutId === existUserPayout.id &&
145
- detail.instituteId === existing.instituteId &&
146
- detail.userId === existing.userId &&
147
- detail.type === existing.type &&
148
- detail.subjectId === existing.subjectId &&
149
- detail.batchId === existing.batchId);
150
- if (found) {
151
- await UserPayoutDetailsModel.update(detail, { where: { id: found.id }, ...options });
152
- }
153
- else {
154
- detail = (await UserPayoutDetailsModel.create(detail, options));
155
- }
156
- userPayoutDetailsList.push(detail);
157
- }
158
- if (userPayoutDetailsList.length) {
159
- await UserPayoutDetailsModel.destroy({
160
- where: {
161
- id: { [sequelize_1.Op.notIn]: userPayoutDetailsList.map(detail => detail.id) },
162
- userPayoutId: existUserPayout.id,
163
- },
164
- ...options,
165
- });
166
- }
167
- const totalPayableAmount = userPayoutDetailsList.reduce((sum, record) => sum + (record.payableAmount || 0), 0);
168
- userPayout.userPayoutDetailsList = userPayoutDetailsList;
169
- userPayout.totalPayableAmount = totalPayableAmount;
170
- return userPayout;
171
- }
172
- catch (err) {
173
- throw err;
174
- }
175
- };
176
- const afterFindHook = async (userPayoutList, options = {}) => {
177
- const transaction = await index_1.sequelize.transaction();
178
- try {
179
- const syncList = JSON.parse(JSON.stringify(userPayoutList));
180
- if (!syncList.length) {
181
- await transaction.commit();
182
- return userPayoutList;
183
- }
184
- const updatedData = [];
185
- for (let syncUserPayout of syncList) {
186
- syncUserPayout = await afterCreateOrUpdateHook(syncUserPayout, undefined, transaction, options);
187
- updatedData.push(syncUserPayout);
188
- }
189
- userPayoutList = userPayoutList.map(payout => {
190
- const found = updatedData.find(one => one.id === payout.id);
191
- if (found) {
192
- payout.totalPayableAmount = found.totalPayableAmount;
193
- payout.userPayoutDetailsList = found.userPayoutDetailsList;
194
- }
195
- return payout;
196
- });
197
- await transaction.commit();
198
- return userPayoutList;
199
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
200
- }
201
- catch (err) {
202
- await transaction.rollback();
203
- return userPayoutList;
204
- }
205
- };
206
- UserPayoutModel.addHook('afterFind', async (result, options) => {
207
- if (options.syncUserPayoutLastCalculatedAt && Array.isArray(result)) {
208
- result = (await afterFindHook(result, options));
209
- }
210
- else if (options.syncUserPayoutLastCalculatedAt && result) {
211
- result = (await afterFindHook([result], options)).pop();
212
- }
213
- });
214
38
  }
215
39
  }
216
40
  UserPayoutModel.init({
@@ -249,9 +73,9 @@ UserPayoutModel.init({
249
73
  field: 'total_paid_amount',
250
74
  },
251
75
  type: {
252
- type: sequelize_1.DataTypes.ENUM,
253
- values: Object.values(app_1.USER_PAYOUT_TYPE),
76
+ type: sequelize_1.DataTypes.STRING,
254
77
  allowNull: false,
78
+ defaultValue: app_1.USER_PAYOUT_TYPE.NONE,
255
79
  },
256
80
  lastCalculatedAt: {
257
81
  type: sequelize_1.DataTypes.DATE,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kipicore/dbcore",
3
- "version": "1.1.473",
3
+ "version": "1.1.474",
4
4
  "description": "Reusable DB core package with Postgres, MongoDB, models, services, interfaces, and types",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",