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

Pass mutations #298

Merged
merged 4 commits into from
Sep 12, 2024
Merged
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
60 changes: 59 additions & 1 deletion platform/local_db_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,62 @@ aws dynamodb put-item \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

aws dynamodb put-item --table-name City --item "{\"cityId\": {\"S\": \"7\"}, \"cityName\": {\"S\": \"Gwalior\"}, \"localTimeZone\": {\"S\": \"GMT+5:30\"}, \"iconUrl\": {\"S\": \"/mount-rushmore.webp\"}, \"countryCode\": {\"S\": \"IN\"}, \"currency\": {\"S\": \"INR\"}}" --return-consumed-capacity TOTAL
aws dynamodb put-item --table-name City --item "{\"cityId\": {\"S\": \"7\"}, \"cityName\": {\"S\": \"Gwalior\"}, \"localTimeZone\": {\"S\": \"GMT+5:30\"}, \"iconUrl\": {\"S\": \"/mount-rushmore.webp\"}, \"countryCode\": {\"S\": \"IN\"}, \"currency\": {\"S\": \"INR\"}}" --return-consumed-capacity TOTAL


aws dynamodb create-table \
--table-name Pass \
--attribute-definitions \
AttributeName=cityId,AttributeType=S \
AttributeName=passId,AttributeType=S \
--key-schema \
AttributeName=cityId,KeyType=HASH \
AttributeName=passId,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--endpoint-url http://localhost:8000


aws dynamodb put-item \
--table-name Pass \
--item "{\"cityId\": {\"S\": \"1\"}, \"passId\": {\"S\": \"pass1\"}, \"passType\": {\"S\": \"LimitedGames\"}, \"totalGames\": {\"N\": \"10\"}, \"totalDays\": {\"N\": \"3000\"}, \"title\": {\"S\": \"10 games pass\"}, \"price\": {\"N\": \"1000\"}, \"status\": {\"S\": \"Active\"}}" \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

aws dynamodb put-item \
--table-name Pass \
--item "{\"cityId\": {\"S\": \"2\"},\"passId\": {\"S\": \"pass2\"},\"passType\": {\"S\": \"LimitedDays\"},\"totalGames\": {\"N\": \"1000\"},\"totalDays\": {\"N\": \"30\"},\"title\": {\"S\": \"30 days pass\"},\"price\": {\"N\": \"1000\"},\"status\": {\"S\": \"Active\"}}" \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

aws dynamodb create-table \
--table-name Subscription \
--attribute-definitions \
AttributeName=userId,AttributeType=S \
AttributeName=subscriptionId,AttributeType=S \
--key-schema \
AttributeName=userId,KeyType=HASH \
AttributeName=subscriptionId,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--endpoint-url http://localhost:8000

aws dynamodb put-item \
--table-name Subscription \
--item "{\"userId\": {\"S\": \"user123\"}, \"subscriptionId\": {\"S\": \"sub123\"}, \"status\": {\"S\": \"Active\"}, \"passId\": {\"S\": \"pass1\"}, \"gamesLeft\": {\"N\": \"10\"}, \"expiryDate\": {\"S\": \"2024-12-31\"}}" \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

aws dynamodb put-item \
--table-name Subscription \
--item "{\"userId\": {\"S\": \"user123\"}, \"subscriptionId\": {\"S\": \"sub123\"}, \"status\": {\"S\": \"Active\"}, \"passId\": {\"S\": \"pass2\"}, \"gamesLeft\": {\"N\": \"10\"}, \"expiryDate\": {\"S\": \"2024-12-31\"}}" \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

aws dynamodb create-table \
--table-name PassPaymentRequest \
--attribute-definitions \
AttributeName=orderId,AttributeType=S \
--key-schema \
AttributeName=orderId,KeyType=HASH \
--billing-mode PAY_PER_REQUEST


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.flickmatch.platform.dynamodb.model;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConvertedEnum;
import lombok.*;

@DynamoDBTable(tableName = "PassPaymentRequest")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PassPaymentRequest {

@DynamoDBHashKey(attributeName="orderId")
private String orderId;
private String location;
private String status;
private String email;
private Double amount;
private String userId;
private String passId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flickmatch.platform.dynamodb.repository;

import com.flickmatch.platform.dynamodb.model.PassPaymentRequest;
import com.flickmatch.platform.dynamodb.model.RazorPaymentRequest;
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

@EnableScan
public interface PassPaymentRequestRepository extends CrudRepository<PassPaymentRequest,String> {
Optional<PassPaymentRequest> findById(String orderId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.flickmatch.platform.graphql.builder;

import com.flickmatch.platform.dynamodb.model.Event;
import com.flickmatch.platform.dynamodb.model.PassPaymentRequest;
import com.flickmatch.platform.dynamodb.model.RazorPaymentRequest;
import com.flickmatch.platform.dynamodb.repository.PassPaymentRequestRepository;
import com.flickmatch.platform.graphql.input.PlayerInput;
import com.flickmatch.platform.graphql.input.RazorPayInput;
import com.razorpay.Order;
import com.razorpay.RazorpayClient;
import com.razorpay.RazorpayException;
import lombok.extern.log4j.Log4j2;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Log4j2
public class PassPaymentRequestBuilder {
@Autowired
PassPaymentRequestRepository passPaymentRequestRepository;


public String createOrderRequest(RazorpayClient razorpayClient, String currency, Double amount) throws RazorpayException {
JSONObject orderRequest = new JSONObject();
orderRequest.put("amount", amount);
orderRequest.put("currency", currency);
Order order = razorpayClient.orders.create(orderRequest);
return order.get("id");
}

public PassPaymentRequest createPaymentRequest(final String orderId,
final String location,
final String userId,
final String passId,
final Double amount,
final String email
) {

PassPaymentRequest paymentRequest = PassPaymentRequest.builder()
.orderId(orderId)
.amount(amount)
.location(location)
.userId(userId)
.status("INITIATED")
.passId(passId)
.email(email)
.build();
return passPaymentRequestRepository.save(paymentRequest);
}

public PassPaymentRequest updatePaymentRequestStatus(final PassPaymentRequest passPaymentRequest,
final boolean isSuccessful) {
if (isSuccessful) {
passPaymentRequest.setStatus("PAID");
} else {
passPaymentRequest.setStatus("FAILED");
}
return passPaymentRequestRepository.save(passPaymentRequest);
}

public PassPaymentRequest getPaymentRequest(final String orderId) {
Optional<PassPaymentRequest> passPaymentRequest = passPaymentRequestRepository.findById(orderId);
return passPaymentRequest.orElseThrow(() -> new IllegalArgumentException("Invalid input"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.flickmatch.platform.dynamodb.repository.SubscriptionRepository;
import com.flickmatch.platform.dynamodb.repository.UserRepository;
import com.flickmatch.platform.graphql.input.CreateSubscriptionInput;
import com.flickmatch.platform.graphql.input.UpdateSubscriptionInput;
import com.flickmatch.platform.graphql.type.MutationResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -55,6 +56,9 @@ public MutationResult createSubscription(String email, String passId) {
}

User user = userOpt.get();
if(user.getHasActiveSubscription()!=false) {
throw new Exception("The user already has an active subscription");
}

Subscription subscription = Subscription.builder()
.passId(passId)
Expand Down Expand Up @@ -111,7 +115,7 @@ public com.flickmatch.platform.graphql.type.Subscription getActiveSubscription(S
throw new Exception("The user with the given email does not exist");
}
User user = userOpt.get();
System.out.println(userOpt.get().getName());
// System.out.println(userOpt.get().getName());
if (!user.getHasActiveSubscription()) {
throw new Exception("The user does not have an active subscription");
}
Expand All @@ -138,7 +142,7 @@ public com.flickmatch.platform.graphql.type.Subscription getActiveSubscription(S
}

com.flickmatch.platform.graphql.type.Subscription mapEventToGQLType(Subscription subs) {
System.out.println(subs.toString());
// System.out.println(subs.toString());

return com.flickmatch.platform.graphql.type.Subscription.builder()
.subscriptionId(subs.getSubscriptionId())
Expand All @@ -150,25 +154,60 @@ com.flickmatch.platform.graphql.type.Subscription mapEventToGQLType(Subscription
.build();
}

// public MutationResult updateSubscription(Subscription subscription) {
// try {
// String passId = subscription.getPassId();
// Optional<Pass> passOpt = passRepository.findByPassId(passId);
// if (passOpt == null) {
// throw new Exception("The pass does not exist");
// }
// return MutationResult.builder()
// .isSuccessful(true)
// .build();
// } catch (Exception e) {
// log.error("Exception occurred:", e);
// log.error("Error finding the pass with the given passId: ", e.getLocalizedMessage());
// return MutationResult.builder()
// .isSuccessful(false)
// .errorMessage(e.getMessage())
// .build();
// }
//
// }
public MutationResult updateSubscription(String subscriptionId) {
try {
Subscription subs = subscriptionRepository.findBySubscriptionId(subscriptionId).get();
String passId = subs.getPassId();
Optional<Pass> passOpt = passRepository.findByPassId(passId);
User user = userRepository.findById(subs.getUserId()).get();
if (passOpt.isEmpty()) {
throw new Exception("The pass does not exist");
}
String type = passOpt.get().getPassType();
if(type.equals("LimitedDays")) {
LocalDate today = LocalDate.now();
String expiryDateString = subs.getExpiryDate();
// Define the formatter for the date string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

// Convert the string to a LocalDate
LocalDate expiryDate = LocalDate.parse(expiryDateString, formatter);
if (today.isAfter(expiryDate)) {
subs.setStatus("Expired");
subs.setGamesLeft(0);
user.setHasActiveSubscription(false);
}
}
else if(type.equals("LimitedGames")) {
Integer totalGamesLeft = subs.getGamesLeft()-1;
// System.out.println("TotalGamesLeft: "+totalGamesLeft);
subs.setGamesLeft(totalGamesLeft);
if(totalGamesLeft==0) {
subs.setStatus("Expired");
subs.setExpiryDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
user.setHasActiveSubscription(false);
}
}
else {
throw new Exception("Invalid pass type");
}
// System.out.println(subs.getGamesLeft());
subscriptionRepository.save(subs);
userRepository.save(user);

return MutationResult.builder()
.isSuccessful(true)
.build();
} catch (Exception e) {
log.error("Exception occurred:", e);
log.error("Error finding the pass with the given passId: ", e.getLocalizedMessage());
return MutationResult.builder()
.isSuccessful(false)
.errorMessage(e.getMessage())
.build();
}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.flickmatch.platform.graphql.controller;

import com.flickmatch.platform.dynamodb.model.User;
import com.flickmatch.platform.dynamodb.repository.UserRepository;
import com.flickmatch.platform.graphql.builder.PassBuilder;
import com.flickmatch.platform.graphql.builder.PassPaymentRequestBuilder;
import com.flickmatch.platform.graphql.builder.RazorPaymentRequestBuilder;
import com.flickmatch.platform.graphql.builder.UserBuilder;
import com.flickmatch.platform.graphql.input.PassPaymentInput;
import com.flickmatch.platform.graphql.type.RazorPayOutput;
import com.flickmatch.platform.proxy.RazorPayProxy;
import com.razorpay.RazorpayClient;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.stereotype.Controller;

import javax.swing.text.html.Option;

@Controller
@Log4j2
public class PassPaymentRequestController {
@Autowired
RazorPaymentRequestBuilder razorPaymentRequestBuilder;

@Autowired
RazorPayProxy razorPayProxy;
@Autowired
PassBuilder passBuilder;
@Autowired
PassPaymentRequestBuilder passPaymentRequestBuilder;
@Autowired
UserRepository userRepository;

@MutationMapping
public RazorPayOutput initiatePassPayment(@Argument PassPaymentInput input) {
try {
RazorpayClient client = razorPayProxy.getRazorPayClient();
Double amount = passBuilder.getAmountForPass(input.getPassId());
abhimanyu-fm marked this conversation as resolved.
Show resolved Hide resolved
String location = input.getLocation();
String currency = input.getCurrency();
String orderId = passPaymentRequestBuilder.createOrderRequest(client,currency, amount);
String email = input.getEmail();
User user = userRepository.findByEmail(input.getEmail()).get();
if(user==null) {
throw new Exception("The user with the given email does not exist");
}
passPaymentRequestBuilder.createPaymentRequest(orderId, location, user.getUserId(), input.getPassId(), amount,email);
return RazorPayOutput.builder().orderId(orderId).isInitiated(true).amount(amount).build();
}
catch (Exception e) {
log.error("Error creating order: {}", e.getMessage());
return RazorPayOutput.builder().isInitiated(false)
.build();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public MutationResult createSubscription(@Argument CreateSubscriptionInput input
String passId = input.getPassId();
return subscriptionBuilder.createSubscription(email, passId);
}
@MutationMapping
public MutationResult updateSubscription(@Argument String subscriptionId) {
return subscriptionBuilder.updateSubscription(subscriptionId);
}

@QueryMapping
public com.flickmatch.platform.graphql.type.Subscription getActiveSubscription(@Argument String email) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flickmatch.platform.graphql.input;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class PassPaymentInput {
private String passId;
private String email;
private String currency;
private String location;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.flickmatch.platform.graphql.input;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class UpdateSubscriptionInput {
private String status;
private String subscriptionId;
private String passId;
private String userId;
private Integer gamesLeft;
private String expiryDate;
}
Loading
Loading