1 import { addMinutes, fromUnixTime } from 'date-fns';
3 import { PRODUCT_BIT, UNPAID_STATE, USER_ROLES } from '../constants';
4 import { hasBit } from '../helpers/bitset';
5 import { decodeBase64URL } from '../helpers/encoding';
6 import type { User } from '../interfaces';
8 const { ADMIN_ROLE, MEMBER_ROLE, FREE_ROLE } = USER_ROLES;
10 export const hasPaidMail = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.MAIL);
11 export const hasPaidDrive = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.DRIVE);
12 export const hasPaidVpn = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.VPN);
13 export const hasPaidPass = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.PASS);
14 export const isPaid = (user: User) => !!user.Subscribed;
15 export const isPrivate = (user: User) => user.Private === 1;
16 export const isFree = (user: User) => !isPaid(user);
17 export const isAdmin = (user: User) => user.Role === ADMIN_ROLE;
18 export const isMember = (user: User) => user.Role === MEMBER_ROLE;
19 export const isSubUser = (user: User) => typeof user.OrganizationPrivateKey !== 'undefined';
20 export const isDelinquent = (user: User) => !!user.Delinquent;
21 export const getHasNonDelinquentScope = (user: User) => user.Delinquent < UNPAID_STATE.DELINQUENT;
22 export const canPay = (user: User) => [ADMIN_ROLE, FREE_ROLE].includes(user.Role) && !isSubUser(user);
24 export const getInfo = (User: User) => {
26 isAdmin: isAdmin(User),
27 isMember: isMember(User),
30 isPrivate: isPrivate(User),
31 isSubUser: isSubUser(User),
32 isDelinquent: isDelinquent(User),
33 hasNonDelinquentScope: getHasNonDelinquentScope(User),
34 hasPaidMail: hasPaidMail(User),
35 hasPaidVpn: hasPaidVpn(User),
36 hasPaidDrive: hasPaidDrive(User),
37 hasPaidPass: hasPaidPass(User),
42 export const formatUser = (User: User) => {
49 export const getUserByte = (user: User) => {
50 const userID = user?.ID || '';
51 const byteCharacters = decodeBase64URL(userID);
52 return byteCharacters.charCodeAt(0);
55 export const getUserAccountAge = (user: User) => {
56 return fromUnixTime(user.CreateTime);
60 * Checks if a user is older than a specified number of minutes.
61 * @param user - The user to check.
62 * @param minutes - The number of minutes to compare against.
63 * @returns `true` if the user is older than the specified number of minutes, `false` otherwise.
65 export const isUserOlderThan = (user: User, minutes: number) => {
66 return new Date() > addMinutes(getUserAccountAge(user), minutes);