001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.payment.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.payment.server.control.PaymentProcessorTransactionCodeControl;
021import com.echothree.model.data.payment.server.entity.PaymentProcessorTransactionCode;
022import com.echothree.util.server.persistence.Session;
023import graphql.annotations.annotationTypes.GraphQLDescription;
024import graphql.annotations.annotationTypes.GraphQLField;
025import graphql.annotations.annotationTypes.GraphQLName;
026import graphql.annotations.annotationTypes.GraphQLNonNull;
027import graphql.schema.DataFetchingEnvironment;
028import java.util.ArrayList;
029import java.util.List;
030
031@GraphQLDescription("payment processor transaction code object")
032@GraphQLName("PaymentProcessorTransactionCode")
033public class PaymentProcessorTransactionCodeObject
034        extends BaseEntityInstanceObject {
035    
036    private final PaymentProcessorTransactionCode paymentProcessorTransactionCode; // Always Present
037    
038    public PaymentProcessorTransactionCodeObject(PaymentProcessorTransactionCode paymentProcessorTransactionCode) {
039        super(paymentProcessorTransactionCode.getPrimaryKey());
040        
041        this.paymentProcessorTransactionCode = paymentProcessorTransactionCode;
042    }
043
044    @GraphQLField
045    @GraphQLDescription("payment processor transaction")
046    public PaymentProcessorTransactionObject getPaymentProcessorTransaction(final DataFetchingEnvironment env) {
047        return PaymentSecurityUtils.getHasPaymentProcessorTransactionAccess(env) ? new PaymentProcessorTransactionObject(paymentProcessorTransactionCode.getPaymentProcessorTransaction()) : null;
048    }
049
050    @GraphQLField
051    @GraphQLDescription("payment processor transaction codes")
052    @GraphQLNonNull
053    public List<PaymentProcessorTransactionCodeObject> getPaymentProcessorTransactionCodes(final DataFetchingEnvironment env) {
054        List<PaymentProcessorTransactionCodeObject> paymentProcessorTransactionCodes = null;
055
056        if(PaymentSecurityUtils.getHasPaymentProcessorTransactionCodesAccess(env)) {
057            var paymentProcessorTransactionCodeControl = Session.getModelController(PaymentProcessorTransactionCodeControl.class);
058            var entities = paymentProcessorTransactionCodeControl.getPaymentProcessorTransactionCodesByPaymentProcessorTransaction(paymentProcessorTransactionCode.getPaymentProcessorTransaction());
059            var objects = new ArrayList<PaymentProcessorTransactionCodeObject>(entities.size());
060
061            entities.forEach((entity) -> objects.add(new PaymentProcessorTransactionCodeObject(entity)));
062
063            paymentProcessorTransactionCodes = objects;
064        }
065
066        return paymentProcessorTransactionCodes;
067    }
068
069}