1 import { addMinutes, fromUnixTime } from 'date-fns';
3 import { UNPAID_STATE } from '@proton/payments';
5 import { PRODUCT_BIT, USER_ROLES } from '../constants';
6 import { hasBit } from '../helpers/bitset';
7 import { decodeBase64URL } from '../helpers/encoding';
8 import type { User } from '../interfaces';
10 const { ADMIN_ROLE, MEMBER_ROLE, FREE_ROLE } = USER_ROLES;
12 export const hasPaidMail = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.MAIL);
13 export const hasPaidDrive = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.DRIVE);
14 export const hasPaidVpn = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.VPN);
15 export const hasPassLifetime = (user: User) => !!user.Flags?.['pass-lifetime'];
16 export const hasPaidPass = (user: User) => hasBit(user.Subscribed, PRODUCT_BIT.PASS) || hasPassLifetime(user);
17 export const isPaid = (user: User) => !!user.Subscribed;
18 export const isPrivate = (user: User) => user.Private === 1;
19 export const isFree = (user: User) => !isPaid(user);
20 export const isAdmin = (user: User) => user.Role === ADMIN_ROLE;
21 export const isMember = (user: User) => user.Role === MEMBER_ROLE;
22 export const isSubUser = (user: User) => typeof user.OrganizationPrivateKey !== 'undefined';
23 export const isDelinquent = (user: User) => !!user.Delinquent;
24 export const getHasNonDelinquentScope = (user: User) => user.Delinquent < UNPAID_STATE.DELINQUENT;
25 export const canPay = (user: User) => [ADMIN_ROLE, FREE_ROLE].includes(user.Role) && !isSubUser(user);
27 export const getInfo = (User: User) => {
29 isAdmin: isAdmin(User),
30 isMember: isMember(User),
33 isPrivate: isPrivate(User),
34 isSubUser: isSubUser(User),
35 isDelinquent: isDelinquent(User),
36 hasNonDelinquentScope: getHasNonDelinquentScope(User),
37 hasPaidMail: hasPaidMail(User),
38 hasPaidVpn: hasPaidVpn(User),
39 hasPaidDrive: hasPaidDrive(User),
40 hasPaidPass: hasPaidPass(User),
41 hasPassLifetime: hasPassLifetime(User),
46 export const formatUser = (User: User) => {
53 export const getUserByte = (user: User) => {
54 const userID = user?.ID || '';
55 const byteCharacters = decodeBase64URL(userID);
56 return byteCharacters.charCodeAt(0);
59 export const getUserAccountAge = (user: User) => {
60 return fromUnixTime(user.CreateTime);
64 * Checks if a user is older than a specified number of minutes.
65 * @param user - The user to check.
66 * @param minutes - The number of minutes to compare against.
67 * @returns `true` if the user is older than the specified number of minutes, `false` otherwise.
69 export const isUserOlderThan = (user: User, minutes: number) => {
70 return new Date() > addMinutes(getUserAccountAge(user), minutes);