Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36418 amit 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
5
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
6
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
7
import com.spice.profitmandi.dao.repository.dtr.EmployeeAttendanceRepository;
8
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
9
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12
import org.springframework.transaction.annotation.Transactional;
13
 
14
import java.time.LocalDate;
15
import java.util.List;
16
 
17
/**
18
 * Read-only DB lookups used by HyperTrackController for endpoints that combine
19
 * a DB read + an external HyperTrack API call. By keeping the reads in a separate
20
 * @Service with @Transactional(readOnly=true), the DB connection is released
21
 * BEFORE the controller makes its external HTTP call — so a slow HyperTrack
22
 * response can no longer pin a DB connection.
23
 */
24
@Service
25
public class HyperTrackService {
26
 
27
    @Autowired
28
    private HypertrackKeyRepository hypertrackKeyRepository;
29
 
30
    @Autowired
31
    private EmployeeAttendanceRepository employeeAttendanceRepository;
32
 
33
    @Autowired
34
    private FofoStoreRepository fofoStoreRepository;
35
 
36
    @Transactional(readOnly = true)
37
    public LocationContext loadLocationContext(int userId, int deviceId) {
38
        HypertrackKey key = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
39
        List<EmployeeAttendance> attendances = employeeAttendanceRepository.selectByUserIdKey(
40
                userId, deviceId, LocalDate.now());
41
        return new LocationContext(key, attendances);
42
    }
43
 
44
    @Transactional(readOnly = true)
45
    public PartnerLocationContext loadPartnerLocationContext(int userId, int deviceId, int fofoId)
46
            throws ProfitMandiBusinessException {
47
        FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
48
        HypertrackKey key = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
49
        return new PartnerLocationContext(fs, key);
50
    }
51
 
52
    public static final class LocationContext {
53
        public final HypertrackKey hypertrackKey;
54
        public final List<EmployeeAttendance> employeeAttendances;
55
        public LocationContext(HypertrackKey k, List<EmployeeAttendance> a) {
56
            this.hypertrackKey = k;
57
            this.employeeAttendances = a;
58
        }
59
    }
60
 
61
    public static final class PartnerLocationContext {
62
        public final FofoStore fofoStore;
63
        public final HypertrackKey hypertrackKey;
64
        public PartnerLocationContext(FofoStore fs, HypertrackKey k) {
65
            this.fofoStore = fs;
66
            this.hypertrackKey = k;
67
        }
68
    }
69
}