Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add purchases to account info #1053

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion db/sql_changeset_schema
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ CREATE SEQUENCE supporters_seq START 1000;
ALTER TABLE supporters ADD PRIMARY KEY (userid);

--- Main primary key orderid + sku (alternative purchasetoken + sku but there could be multiple orderid for same purchaseToken)
CREATE TABLE supporters_device_sub(sku text, purchasetoken text, prevvalidpurchasetoken text, timestamp timestamp,
CREATE TABLE supporters_device_sub(userid int, sku text, purchasetoken text, prevvalidpurchasetoken text, timestamp timestamp,
autorenewing boolean, starttime timestamp, expiretime timestamp, kind text, payload text, orderid text,
price int, pricecurrency text, introprice int, intropricecurrency text, introcycles int, introcyclename text,
paymentstate int, valid boolean, checktime timestamp);
CREATE INDEX supporters_device_sub_starttime_idx on supporters_device_sub(starttime);
CREATE INDEX supporters_device_sub_expiretime_idx on supporters_device_sub(expiretime);
CREATE INDEX supporters_device_sub_orderid_idx on supporters_device_sub(orderid);
CREATE INDEX supporters_device_sub_userid_idx on supporters_device_sub(userid);
ALTER TABLE supporters_device_sub add primary key (sku, orderid);

------ PREMIUM accounts ----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.*;

import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -25,6 +19,8 @@ public interface DeviceSubscriptionsRepository extends JpaRepository<SupporterDe

List<SupporterDeviceSubscription> findFirst5BySkuOrderByStarttimeDesc(String sku);

List<SupporterDeviceSubscription> findAllByUserid(int userId);

// PRIMARY KEY is (orderId + SKU) or (purchaseToken + SKU), orderId could be restored from purchaseToken and sku
@Entity
@Table(name = "supporters_device_sub")
Expand Down Expand Up @@ -81,6 +77,9 @@ public class SupporterDeviceSubscription implements Serializable {

@Column(name = "introcycles")
public Integer introcycles ;

@Column(name = "userid")
public Integer userid;
}

public class SupporterDeviceSubscriptionPrimaryKey implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.transaction.Transactional;

import net.osmand.server.WebSecurityConfiguration;
import net.osmand.server.api.repo.DeviceSubscriptionsRepository;
import net.osmand.shared.gpx.GpxFile;
import net.osmand.shared.gpx.GpxUtilities;
import net.osmand.shared.io.KFile;
Expand Down Expand Up @@ -102,6 +103,9 @@ public class UserdataService {
@Autowired
EmailSenderService emailSender;

@Autowired
DeviceSubscriptionsRepository subscriptionsRepository;

@Autowired
protected PremiumUserDevicesRepository devicesRepository;

Expand Down Expand Up @@ -1266,4 +1270,22 @@ private void processGpxFile(PremiumUserDevicesRepository.PremiumUserDevice dev,
}
}
}

public ResponseEntity<String> addPurchase(String code, PremiumUserDevicesRepository.PremiumUserDevice dev) {
List<DeviceSubscriptionsRepository.SupporterDeviceSubscription> purchases = subscriptionsRepository.findByOrderId(code);
if (purchases.isEmpty()) {
return ResponseEntity.ok("No purchase found");
}
if (purchases.size() > 1) {
return ResponseEntity.ok("Multiple purchases found");
}
DeviceSubscriptionsRepository.SupporterDeviceSubscription purchase = purchases.get(0);
if (purchase.userid != null) {
return ResponseEntity.ok("Purchase already added");
}
purchase.userid = dev.userid;
subscriptionsRepository.saveAndFlush(purchase);

return ResponseEntity.ok("Purchase added");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.osmand.server.api.services.DownloadIndexesService.ServerCommonFile;

import net.osmand.server.controllers.user.MapApiController;
import net.osmand.server.utils.MultiPlatform;
import net.osmand.server.utils.exception.OsmAndPublicApiException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -81,6 +82,9 @@ public class UserdataController {
@Autowired
protected UserSubscriptionService userSubService;

@Autowired
OsmAndMapsService osmAndMapsService;

@Autowired
EmailSenderService emailSender;

Expand Down Expand Up @@ -441,4 +445,16 @@ public ResponseEntity<String> confirmCode(@RequestBody MapApiController.UserPass
}
return ResponseEntity.badRequest().body("Please enter valid email");
}

@MultiPlatform
@PostMapping(path = {"/add-purchase"}, produces = "application/json")
public ResponseEntity<String> addPurchase(@RequestBody String code,
@RequestParam(name = "deviceid", required = false) Integer deviceId,
@RequestParam(required = false) String accessToken) {
PremiumUserDevice dev = (deviceId != null && accessToken != null) ? checkToken(deviceId, accessToken) : osmAndMapsService.checkUser();
if (dev == null) {
return userdataService.tokenNotValidResponse();
}
return ResponseEntity.ok(gson.toJson(userdataService.addPurchase(code, dev)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ public ResponseEntity<String> getAccountInfo() {
final String EXPIRE_TIME_KEY = "expireTime";
final String MAX_ACCOUNT_SIZE = "maxAccSize";
final String NICKNAME = "nickname";
final String PURCHASES = "purchases";

PremiumUserDevice dev = osmAndMapsService.checkUser();
PremiumUsersRepository.PremiumUser pu = usersRepository.findById(dev.userid);
Expand All @@ -618,6 +619,10 @@ public ResponseEntity<String> getAccountInfo() {
info.put(EXPIRE_TIME_KEY, prepareExpireTime.toString());
info.put(MAX_ACCOUNT_SIZE, String.valueOf((MAXIMUM_ACCOUNT_SIZE)));
}
List<DeviceSubscriptionsRepository.SupporterDeviceSubscription> purchases = subscriptionsRepo.findAllByUserid(dev.userid);
info.put(PURCHASES, gson.toJson(purchases.stream()
.map(s -> Map.of(TYPE_SUB, s.sku, START_TIME_KEY, s.starttime, EXPIRE_TIME_KEY, s.expiretime))
.toList()));
}
return ResponseEntity.ok(gson.toJson(Collections.singletonMap(INFO_KEY, info)));
}
Expand Down