import { EmptyResultError } from '../errors'; import { AcquisitionContext } from './acquisition-context'; import { ProductDetails, AdministratorDetails } from '../types/licence'; const VALID_LICENCE = 'active'; const B2B_MARKETING_INITIATIVE = 'b2b-marketing-initiative'; const B2C_PARTNERSHIP = 'b2c-partnership'; const BUSINESS_DEVELOPMENT = 'business-development'; const COMPLIMENTARY = 'complimentary'; const CONTRACT = 'contract'; const DEVELOPER_PORTAL = 'developer-portal'; const LONGROOM = 'longroom'; const NEWSPAPER_BUNDLE = 'newspaper-bundle'; const PROSPECT = 'prospect'; const SUBSCRIPTION = 'subscription'; const TRIAL = 'trial'; const SWG = 'Subscribed with Google'; /** Class representing a licence. */ export class Licence { public id?: string; public allocatedSeats?: number; public seatLimit?: number; public status?: string; public types?: Array; public isB2c?: boolean; public isB2b?: boolean; public isSso?: boolean; public acquisitionContext?: Array; public userId?: string; public joinedDate?: string; public seatExpiryDate?: string; public linkedContractId?: string; public links?: Array; public products?: Array; public administrators?: Array; public issueReason?: string; /** * Licence Details * @param licenceData mapped AIM GraphQL licence data. */ constructor(licenceData?: Partial) { if (licenceData) { Object.assign(this, licenceData); } } /** * Is Subscribed with Google * @returns true|false */ public isSubscribedWithGoogle(): boolean { return this.issueReason === SWG; } /** * Is this licence valid * @returns true|false */ public isValid(): boolean { return this.status === VALID_LICENCE; } /** * Is this licence type a managed trial * @returns true|false */ public isManagedTrial(): boolean { return (this.types || []).includes(TRIAL); } /** * Is this licence a type a prospect trial * @returns true|false */ public isProspectTrial(): boolean { return (this.types || []).includes(B2B_MARKETING_INITIATIVE); } /** * Is this licence type contract, newspaper-bundle, complimentary, business-development or prospect * @returns true|false */ public isCompanyLicence(): boolean { return this.isContractLicence() || this.isBundleLicence() || this.isComplimentaryLicence() || this.isBusinessDevelopmentLicence() || this.isProspectLicence(); } /** * Is this licence type contract * @returns true|false */ public isContractLicence(): boolean { return (this.types || []).includes(CONTRACT); } /** * Is this licence type newspaper-bundle * @returns true|false */ public isBundleLicence(): boolean { return (this.types || []).includes(NEWSPAPER_BUNDLE); } /** * Is this licence type complimentary * @returns true|false */ public isComplimentaryLicence(): boolean { return (this.types || []).includes(COMPLIMENTARY); } /** * Is this licence type business-development * @returns true|false */ public isBusinessDevelopmentLicence(): boolean { return (this.types || []).includes(BUSINESS_DEVELOPMENT); } /** * Is this licence type prospect * @returns true|false */ public isProspectLicence(): boolean { return (this.types || []).includes(PROSPECT); } /** * Is this licence type subscription * @returns true|false */ public isSubscriptionLicence(): boolean { return (this.types || []).includes(SUBSCRIPTION); } /** * Is this licence type b2c-partnership * @returns true|false */ public isB2cPartnershipLicence(): boolean { return (this.types || []).includes(B2C_PARTNERSHIP); } /** * Is this licence type longroom * @returns true|false */ public isLongRoomLicence(): boolean { return (this.types || []).includes(LONGROOM); } /** * Is this licence type developer-portal * @returns true|false */ public isDeveloperPortalLicence(): boolean { return (this.types || []).includes(DEVELOPER_PORTAL); } /** * Return the first AcquisitionContext in the array of contexts * This could be expanded to check the contexts referrer is correct but this current isn't needed * @throws {EmptyResultError} When there are no acquisition contexts * @returns Acquisition Context */ public getAcquisitionContext(): AcquisitionContext { if (!Array.isArray(this.acquisitionContext) || this.acquisitionContext.length < 1) { throw new EmptyResultError('Acquisition Context Missing Error', { licenceId: this.id }); } return this.acquisitionContext[0]; } /** * Is the email domain allowed * @param email * @returns true|false */ public isAllowedEmailDomain(email: string): boolean { return this.getAcquisitionContext().isAllowedEmailDomain(email); } /** * Is the email domain blocked * @param email * @returns true|false */ public isBlockedEmailDomain(email: string): boolean { return this.getAcquisitionContext().isBlockedEmailDomain(email); } /** * Is the current acquisition context a marketing campaign * @returns true|false */ public isMarketingCampaign(): boolean { return this.getAcquisitionContext().isMarketingCampaign(); } /** * Is there a seat available on the license? * @returns true|false */ public isSeatAvailable(): boolean { if (this.allocatedSeats && this.seatLimit) { return this.allocatedSeats < this.seatLimit; } else { return true; } } /** * Is the user an administrator of this licence * @returns true|false */ public isUserAnAdminstrator(userId: string): boolean { const administratorDetails = this.administrators && this.administrators.find(administrator => administrator.userId === userId ); if (!administratorDetails) { return false; } return administratorDetails.licenceId === this.id; } }