001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.user.server.graphql;
018
019import com.echothree.model.control.accounting.server.graphql.AccountingSecurityUtils;
020import com.echothree.model.control.accounting.server.graphql.CurrencyObject;
021import com.echothree.model.control.campaign.server.control.CampaignControl;
022import com.echothree.model.control.campaign.server.graphql.UserVisitCampaignObject;
023import com.echothree.model.control.graphql.server.graphql.TimeObject;
024import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
025import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
026import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
027import com.echothree.model.control.graphql.server.util.BaseGraphQl;
028import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
029import com.echothree.model.control.offer.server.graphql.OfferSecurityUtils;
030import com.echothree.model.control.offer.server.graphql.OfferUseObject;
031import com.echothree.model.control.party.server.graphql.DateTimeFormatObject;
032import com.echothree.model.control.party.server.graphql.LanguageObject;
033import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
034import com.echothree.model.control.party.server.graphql.TimeZoneObject;
035import com.echothree.model.data.campaign.common.UserVisitCampaignConstants;
036import com.echothree.model.data.user.server.entity.UserVisit;
037import com.echothree.util.server.persistence.Session;
038import graphql.annotations.annotationTypes.GraphQLDescription;
039import graphql.annotations.annotationTypes.GraphQLField;
040import graphql.annotations.annotationTypes.GraphQLName;
041import graphql.annotations.annotationTypes.GraphQLNonNull;
042import graphql.annotations.connection.GraphQLConnection;
043import graphql.schema.DataFetchingEnvironment;
044import java.util.ArrayList;
045import java.util.stream.Collectors;
046
047@GraphQLDescription("user visit object")
048@GraphQLName("UserVisit")
049public class UserVisitObject
050        implements BaseGraphQl {
051
052    private final UserVisit userVisit; // Always Present
053
054    public UserVisitObject(UserVisit userVisit) {
055        this.userVisit = userVisit;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("user visit group")
060    public UserVisitGroupObject getUserVisitGroup(final DataFetchingEnvironment env) {
061        var userVisitGroup = userVisit.getUserVisitGroup();
062
063        return userVisitGroup != null && UserSecurityUtils.getHasUserVisitGroupAccess(env) ?
064                new UserVisitGroupObject(userVisit.getUserVisitGroup()) : null;
065    }
066
067    @GraphQLField
068    @GraphQLDescription("user key")
069    public UserKeyObject getUserKey() {
070        var userKey = userVisit.getUserKey();
071
072        return userKey == null ? null : new UserKeyObject(userKey);
073    }
074
075    @GraphQLField
076    @GraphQLDescription("preferred language")
077    public LanguageObject getPreferredLanguage(final DataFetchingEnvironment env) {
078        var preferredLanguage = userVisit.getPreferredLanguage();
079
080        return preferredLanguage != null && PartySecurityUtils.getHasLanguageAccess(env) ? new LanguageObject(preferredLanguage) : null;
081    }
082
083    @GraphQLField
084    @GraphQLDescription("preferred language")
085    public CurrencyObject getPreferredCurrency(final DataFetchingEnvironment env) {
086        var preferredCurrency = userVisit.getPreferredCurrency();
087
088        return preferredCurrency != null && AccountingSecurityUtils.getHasCurrencyAccess(env) ? new CurrencyObject(preferredCurrency) : null;
089    }
090
091    @GraphQLField
092    @GraphQLDescription("preferred time zone")
093    public TimeZoneObject getPreferredTimeZone(final DataFetchingEnvironment env) {
094        var preferredTimeZone = userVisit.getPreferredTimeZone();
095
096        return preferredTimeZone != null && PartySecurityUtils.getHasTimeZoneAccess(env) ? new TimeZoneObject(preferredTimeZone) : null;
097    }
098
099    @GraphQLField
100    @GraphQLDescription("preferred date time format")
101    public DateTimeFormatObject getPreferredDateTimeFormat(final DataFetchingEnvironment env) {
102        var preferredDateTimeFormat = userVisit.getPreferredDateTimeFormat();
103
104        return preferredDateTimeFormat != null && PartySecurityUtils.getHasDateTimeFormatAccess(env) ? new DateTimeFormatObject(preferredDateTimeFormat) : null;
105    }
106
107    @GraphQLField
108    @GraphQLDescription("last command time")
109    @GraphQLNonNull
110    public TimeObject getLastCommandTime(final DataFetchingEnvironment env) {
111        return new TimeObject(userVisit.getLastCommandTime());
112    }
113    
114    @GraphQLField
115    @GraphQLDescription("offer use")
116    public OfferUseObject getOfferUse(final DataFetchingEnvironment env) {
117        var offeruse = userVisit.getOfferUse();
118
119        return offeruse != null && OfferSecurityUtils.getHasOfferUseAccess(env) ? new OfferUseObject(userVisit.getOfferUse()) : null;
120    }
121
122//    @GraphQLField
123//    @GraphQLDescription("associate referral")
124//    public AssociateReferralObject getAssociateReferral() {
125//        var associateReferral = userVisit.getAssociateReferral();
126//
127//        return associateReferral != null && AssociateSecurityUtils.getHasAssociateReferralAccess(env) ? new AssociateReferralObject(associateReferral) : null;
128//    }
129
130    @GraphQLField
131    @GraphQLDescription("retain until time")
132    public TimeObject getRetainUntilTime(final DataFetchingEnvironment env) {
133        var retainUntilTime = userVisit.getRetainUntilTime();
134
135        return retainUntilTime == null ? null : new TimeObject(retainUntilTime);
136    }
137
138    @GraphQLField
139    @GraphQLDescription("user visit campaigns")
140    @GraphQLNonNull
141    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
142    public CountingPaginatedData<UserVisitCampaignObject> getUserVisitCampaigns(final DataFetchingEnvironment env) {
143//        if(CampaignSecurityUtils.getHasUserVisitCampaignsAccess(env)) {
144            var campaignControl = Session.getModelController(CampaignControl.class);
145            var totalCount = campaignControl.countUserVisitCampaignsByUserVisit(userVisit);
146
147            try(var objectLimiter = new ObjectLimiter(env, UserVisitCampaignConstants.COMPONENT_VENDOR_NAME, UserVisitCampaignConstants.ENTITY_TYPE_NAME, totalCount)) {
148                var entities = campaignControl.getUserVisitCampaignsByUserVisit(userVisit);
149                var items = entities.stream().map(UserVisitCampaignObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
150
151                return new CountedObjects<>(objectLimiter, items);
152            }
153//        } else {
154//            return Connections.emptyConnection();
155//        }
156    }
157
158}