Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
import com.spice.profitmandi.dao.repository.dtr.EmployeeAttendanceRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * Read-only DB lookups used by HyperTrackController for endpoints that combine
 * a DB read + an external HyperTrack API call. By keeping the reads in a separate
 * @Service with @Transactional(readOnly=true), the DB connection is released
 * BEFORE the controller makes its external HTTP call — so a slow HyperTrack
 * response can no longer pin a DB connection.
 */
@Service
public class HyperTrackService {

    @Autowired
    private HypertrackKeyRepository hypertrackKeyRepository;

    @Autowired
    private EmployeeAttendanceRepository employeeAttendanceRepository;

    @Autowired
    private FofoStoreRepository fofoStoreRepository;

    @Transactional(readOnly = true)
    public LocationContext loadLocationContext(int userId, int deviceId) {
        HypertrackKey key = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
        List<EmployeeAttendance> attendances = employeeAttendanceRepository.selectByUserIdKey(
                userId, deviceId, LocalDate.now());
        return new LocationContext(key, attendances);
    }

    @Transactional(readOnly = true)
    public PartnerLocationContext loadPartnerLocationContext(int userId, int deviceId, int fofoId)
            throws ProfitMandiBusinessException {
        FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
        HypertrackKey key = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
        return new PartnerLocationContext(fs, key);
    }

    public static final class LocationContext {
        public final HypertrackKey hypertrackKey;
        public final List<EmployeeAttendance> employeeAttendances;
        public LocationContext(HypertrackKey k, List<EmployeeAttendance> a) {
            this.hypertrackKey = k;
            this.employeeAttendances = a;
        }
    }

    public static final class PartnerLocationContext {
        public final FofoStore fofoStore;
        public final HypertrackKey hypertrackKey;
        public PartnerLocationContext(FofoStore fs, HypertrackKey k) {
            this.fofoStore = fs;
            this.hypertrackKey = k;
        }
    }
}