|
| 1 | +/* eslint-disable prefer-const */ |
| 2 | +import { format } from 'date-fns' |
| 3 | +import { |
| 4 | + checkUserLoggedIn, |
| 5 | +} from '../helpers/user.helpers' |
| 6 | +import { Organization } from '../models/organization.model' |
| 7 | +import { RoleOfUser, User, UserInterface } from '../models/user' |
| 8 | +import { Context } from './../context' |
| 9 | + |
| 10 | +export type OrganizationType = InstanceType<typeof Organization> |
| 11 | +export type UserType = InstanceType<typeof User> |
| 12 | + |
| 13 | +interface RegistrationDataStatsInterface { |
| 14 | + month: |
| 15 | + | 'jan' |
| 16 | + | 'feb' |
| 17 | + | 'mar' |
| 18 | + | 'apr' |
| 19 | + | 'may' |
| 20 | + | 'jun' |
| 21 | + | 'jul' |
| 22 | + | 'aug' |
| 23 | + | 'sep' |
| 24 | + | 'oct' |
| 25 | + | 'nov' |
| 26 | + | 'dec' |
| 27 | + | null; |
| 28 | + users: number | null; |
| 29 | + organizations: number | null; |
| 30 | +} |
| 31 | + |
| 32 | +interface RegistrationDataInterface { |
| 33 | + year: number; |
| 34 | + stats: RegistrationDataStatsInterface[]; |
| 35 | +} |
| 36 | +const months = [ |
| 37 | + 'jan', |
| 38 | + 'feb', |
| 39 | + 'mar', |
| 40 | + 'apr', |
| 41 | + 'may', |
| 42 | + 'jun', |
| 43 | + 'jul', |
| 44 | + 'aug', |
| 45 | + 'sep', |
| 46 | + 'oct', |
| 47 | + 'nov', |
| 48 | + 'dec', |
| 49 | +]; |
| 50 | + |
| 51 | +const calcYearRegData = (usersOrgsData: any, yearsRegData: RegistrationDataInterface[], isUsers: boolean) => { |
| 52 | + for (const userOrg of usersOrgsData) { |
| 53 | + if (userOrg.createdAt) { |
| 54 | + const initialMonthsData: RegistrationDataStatsInterface[] = months.map((month) => ({ |
| 55 | + month: month as RegistrationDataStatsInterface['month'], |
| 56 | + users: null, |
| 57 | + organizations: null, |
| 58 | + })); |
| 59 | + |
| 60 | + const createdAt = new Date(userOrg.createdAt) |
| 61 | + const regYear = createdAt.getFullYear(); |
| 62 | + const regMonthNum = createdAt.getMonth(); |
| 63 | + const regMonth = format(createdAt, 'MMM').toLowerCase() as RegistrationDataStatsInterface['month']; |
| 64 | + |
| 65 | + const yearIndex = yearsRegData.findIndex(year => year.year === regYear); |
| 66 | + |
| 67 | + if (yearIndex !== -1) { |
| 68 | + const statMonthIndex = yearsRegData[yearIndex].stats.findIndex((month: { month: string | null }) => month.month === regMonth); |
| 69 | + if (statMonthIndex !== -1) { |
| 70 | + const tempValue = isUsers ? yearsRegData[yearIndex].stats[statMonthIndex].users : yearsRegData[yearIndex].stats[statMonthIndex].organizations; |
| 71 | + if (isUsers) { |
| 72 | + yearsRegData[yearIndex].stats[statMonthIndex].users = (tempValue && tempValue > 0) ? tempValue + 1 : 1; |
| 73 | + } else { |
| 74 | + yearsRegData[yearIndex].stats[statMonthIndex].organizations = (tempValue && tempValue > 0) ? tempValue + 1 : 1 |
| 75 | + } |
| 76 | + } else { |
| 77 | + const statMonthIndex2 = initialMonthsData.findIndex(month => month.month === regMonth); |
| 78 | + initialMonthsData[statMonthIndex2] = { month: regMonth, users: isUsers ? 1 : null, organizations: !isUsers ? 1 : null } |
| 79 | + initialMonthsData.forEach(monthData => { |
| 80 | + const monthIndex = months.findIndex((month) => monthData.month === month); |
| 81 | + if (monthIndex !== -1 && monthIndex < regMonthNum) { |
| 82 | + monthData.users = (monthData.users != null && monthData.users >= 0) ? monthData.users : 0 |
| 83 | + monthData.organizations = (monthData.organizations != null && monthData.organizations >= 0) ? monthData.organizations : 0 |
| 84 | + } |
| 85 | + }) |
| 86 | + yearsRegData[yearIndex].stats = initialMonthsData; |
| 87 | + } |
| 88 | + } else { |
| 89 | + const statMonthIndex = initialMonthsData.findIndex(month => month.month === regMonth); |
| 90 | + if (statMonthIndex !== -1) { |
| 91 | + initialMonthsData[statMonthIndex] = { month: regMonth, users: isUsers ? 1 : null, organizations: !isUsers ? 1 : null } |
| 92 | + initialMonthsData.forEach(monthData => { |
| 93 | + const monthIndex = months.findIndex((month) => monthData.month === month); |
| 94 | + if (monthIndex !== -1 && monthIndex < regMonthNum) { |
| 95 | + monthData.users = 0 |
| 96 | + monthData.organizations = 0 |
| 97 | + } |
| 98 | + }) |
| 99 | + yearsRegData.push({ |
| 100 | + year: regYear, |
| 101 | + stats: [...initialMonthsData], |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return yearsRegData |
| 108 | +}; |
| 109 | + |
| 110 | +const resolvers: any = { |
| 111 | + Query: { |
| 112 | + async getAllOrgUsers(_: any, __: any, context: Context) { |
| 113 | + ; (await checkUserLoggedIn(context))([RoleOfUser.SUPER_ADMIN]); |
| 114 | + |
| 115 | + const totalUsers = await User.countDocuments(); |
| 116 | + const allOrgUsers = { |
| 117 | + totalUsers: totalUsers, |
| 118 | + organizations: [] as any[] |
| 119 | + } |
| 120 | + |
| 121 | + const orgs = await Organization.find({ status: 'active' }); |
| 122 | + |
| 123 | + for (const org of orgs) { |
| 124 | + const orgUsers = await User.find({ organizations: org.name }); |
| 125 | + |
| 126 | + const startOfMonth = new Date(); |
| 127 | + startOfMonth.setDate(1); |
| 128 | + startOfMonth.setHours(0, 0, 0, 0); |
| 129 | + |
| 130 | + const endOfMonth = new Date(); |
| 131 | + endOfMonth.setMonth(startOfMonth.getMonth() + 1); |
| 132 | + endOfMonth.setDate(0); |
| 133 | + endOfMonth.setHours(23, 59, 59, 999); |
| 134 | + |
| 135 | + const monthlyRegistrations = await User.countDocuments({ |
| 136 | + createdAt: { $gte: startOfMonth, $lte: endOfMonth }, |
| 137 | + organizations: org.name |
| 138 | + }); |
| 139 | + |
| 140 | + const recentLogin = org.logins.find((count) => { |
| 141 | + return count.date.toISOString().split('T')[0] === new Date().toISOString().split('T')[0] |
| 142 | + }) |
| 143 | + const result = { |
| 144 | + organization: org, |
| 145 | + members: orgUsers, |
| 146 | + loginsCount: recentLogin?.loginsCount || 0, |
| 147 | + recentLocation: recentLogin?.recentLocation, |
| 148 | + monthPercentage: orgUsers.length > 0 ? (monthlyRegistrations / orgUsers.length) * 100 : 0 |
| 149 | + } |
| 150 | + allOrgUsers.organizations.push(result); |
| 151 | + } |
| 152 | + |
| 153 | + return allOrgUsers |
| 154 | + }, |
| 155 | + async getRegistrationStats(_: any, __: any, context: Context) { |
| 156 | + ; (await checkUserLoggedIn(context))([RoleOfUser.SUPER_ADMIN]); |
| 157 | + |
| 158 | + const months = [ |
| 159 | + 'jan', |
| 160 | + 'feb', |
| 161 | + 'mar', |
| 162 | + 'apr', |
| 163 | + 'may', |
| 164 | + 'jun', |
| 165 | + 'jul', |
| 166 | + 'aug', |
| 167 | + 'sep', |
| 168 | + 'oct', |
| 169 | + 'nov', |
| 170 | + 'dec', |
| 171 | + ]; |
| 172 | + |
| 173 | + const monthsData: any = []; |
| 174 | + let yearsRegData: RegistrationDataInterface[] = [{ |
| 175 | + year: new Date().getFullYear(), |
| 176 | + stats: monthsData |
| 177 | + }]; |
| 178 | + |
| 179 | + const users = await User.find(); |
| 180 | + yearsRegData = calcYearRegData(users, yearsRegData, true); |
| 181 | + |
| 182 | + const organizations = await Organization.find(); |
| 183 | + yearsRegData = calcYearRegData(organizations, yearsRegData, false); |
| 184 | + |
| 185 | + return yearsRegData |
| 186 | + } |
| 187 | + }, |
| 188 | +} |
| 189 | +export default resolvers |
0 commit comments