Sfoglia il codice sorgente

list total credits

Mauricio Araujo 2 anni fa
parent
commit
71e1edcb20

+ 2 - 2
dashboard/src/lib/hooks/useStripe.tsx

@@ -38,7 +38,7 @@ type TGetPublishableKey = {
 };
 
 type TGetCredits = {
-  creditGrantsList: CreditGrantList;
+  totalCredits: number;
 };
 
 type TGetPlan = {
@@ -208,7 +208,7 @@ export const usePorterCredits = (): TGetCredits => {
   );
 
   return {
-    creditGrantsList: creditsReq.data,
+    totalCredits: creditsReq.data,
   };
 };
 

+ 9 - 21
dashboard/src/main/home/project-settings/BillingPage.tsx

@@ -29,7 +29,7 @@ function BillingPage(): JSX.Element {
   const [shouldCreate, setShouldCreate] = useState(false);
   const { currentProject } = useContext(Context);
 
-  const { creditGrantsList } = usePorterCredits();
+  const { totalCredits } = usePorterCredits();
   const { plan } = useCustomerPlan();
 
   const {
@@ -105,26 +105,14 @@ function BillingPage(): JSX.Element {
           </Text>
           <Spacer y={1} />
 
-          <div>
-            {creditGrantsList === undefined ? (
-              <Loading></Loading>
-            ) : creditGrantsList.length === 0 ? (
-              <div>No credit grants available.</div>
-            ) : (
-              creditGrantsList.map((grant) => (
-                <Container>
-                  <Image src={gift} style={{ marginTop: "-2px" }} />
-                  <Spacer inline x={1} />
-                  <Text size={20}>
-                    {grant.balance.including_pending > 0
-                      ? `$${formatCredits(grant.balance.including_pending)}`
-                      : "$ 0.00"}
-                  </Text>
-                </Container>
-              ))
-            )}
-            <Spacer y={1} />
-          </div>
+          <Container>
+            <Image src={gift} style={{ marginTop: "-2px" }} />
+            <Spacer inline x={1} />
+            <Text size={20}>
+              {totalCredits > 0 ? `$${formatCredits(totalCredits)}` : "$ 0.00"}
+            </Text>
+          </Container>
+          <Spacer y={1} />
 
           <Text size={16}>Plan Details</Text>
           <Spacer y={1} />

+ 8 - 3
internal/billing/metronome.go

@@ -176,8 +176,8 @@ func (m MetronomeClient) EndCustomerPlan(ctx context.Context, customerID uuid.UU
 	return nil
 }
 
-// ListCustomerCredits will return the list of credit grants for the customer
-func (m MetronomeClient) ListCustomerCredits(ctx context.Context, customerID uuid.UUID) (credits []types.CreditGrant, err error) {
+// ListCustomerCredits will return the total number of credits for the customer
+func (m MetronomeClient) ListCustomerCredits(ctx context.Context, customerID uuid.UUID) (credits float64, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "list-customer-credits")
 	defer span.End()
 
@@ -202,7 +202,12 @@ func (m MetronomeClient) ListCustomerCredits(ctx context.Context, customerID uui
 		return credits, telemetry.Error(ctx, span, err, "failed to list customer credits")
 	}
 
-	return result.Data, nil
+	var totalCredits float64
+	for _, grant := range result.Data {
+		totalCredits += grant.Balance.IncludingPending
+	}
+
+	return totalCredits, nil
 }
 
 func do(method string, path string, apiKey string, body interface{}, data interface{}) (err error) {