Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Fixed usages and billing page
Acho Arnoldeefa2f52f6ee
diff --git a/api/pkg/entities/billing_usage.go b/api/pkg/entities/billing_usage.goindex 48096ff5..3b5e77f9 100644--- a/api/pkg/entities/billing_usage.go+++ b/api/pkg/entities/billing_usage.go@@ -12,6 +12,7 @@ type BillingUsage struct {UserID UserID `json:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`SentMessages uint `json:"sent_messages" example:"321"`ReceivedMessages uint `json:"received_messages" example:"465"`+ TotalCost uint `json:"total_cost" example:"0"`StartTimestamp time.Time `json:"start_timestamp" example:"2022-01-01T00:00:00+00:00"`EndTimestamp time.Time `json:"end_timestamp" example:"2022-01-31T23:59:59+00:00"`CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`diff --git a/api/pkg/repositories/gorm_billing_usage_repository.go b/api/pkg/repositories/gorm_billing_usage_repository.goindex ecc3cd90..62211618 100644--- a/api/pkg/repositories/gorm_billing_usage_repository.go+++ b/api/pkg/repositories/gorm_billing_usage_repository.go@@ -87,13 +87,17 @@ func (repository *gormBillingUsageRepository) GetCurrent(ctx context.Context, userr := crdbgorm.ExecuteTx(ctx, repository.db, nil,func(tx *gorm.DB) error {+ loadedUsage := &entities.BillingUsage{}result := tx.WithContext(ctx).+ Where("user_id = ?", userID).Where("start_timestamp = ?", now.New(timestamp).BeginningOfMonth()).- First(usage)+ First(&loadedUsage)if errors.Is(result.Error, gorm.ErrRecordNotFound) {return tx.WithContext(ctx).Create(usage).Error}++ *usage = *loadedUsagereturn result.Error},)diff --git a/web/components/MessageThreadHeader.vue b/web/components/MessageThreadHeader.vueindex 2c8bd948..ddb80f1a 100644--- a/web/components/MessageThreadHeader.vue+++ b/web/components/MessageThreadHeader.vue@@ -137,6 +137,18 @@</v-list-item-title></v-list-item-content></v-list-item>+ <v-list-item :to="{ name: 'billing' }" exact>+ <v-list-item-icon class="pl-2">+ <v-icon dense>{{ mdiFinance }}</v-icon>+ </v-list-item-icon>+ <v-list-item-content class="ml-n3">+ <v-list-item-title class="pr-16 py-1">+ <span :class="{ 'pr-16': $vuetify.breakpoint.mdAndUp }">+ Usage & Billing+ </span>+ </v-list-item-title>+ </v-list-item-content>+ </v-list-item><v-list-item @click.prevent="logout"><v-list-item-icon class="pl-2"><v-icon dense>{{ mdiLogout }}</v-icon>@@ -162,6 +174,7 @@ import {mdiAccountCog,mdiLogout,mdiDownload,+ mdiFinance,mdiPackageUp,mdiPackageDown,mdiDotsVertical,@@ -178,6 +191,7 @@ export default class MessageThreadHeader extends Vue {mdiLogout = mdiLogoutmdiDownload = mdiDownloadmdiPackageUp = mdiPackageUp+ mdiFinance = mdiFinancemdiPackageDown = mdiPackageDownmdiDotsVertical = mdiDotsVerticalmdiCircle = mdiCirclediff --git a/web/models/billing.ts b/web/models/billing.tsnew file mode 100644index 00000000..37730e06--- /dev/null+++ b/web/models/billing.ts@@ -0,0 +1,10 @@+export interface BillingUsage {+ id: string+ start_timestamp: string+ end_timestamp: string+ user_id: string+ sent_messages: number+ received_messages: number+ total_cost: number+ created_at: string+}diff --git a/web/pages/billing/index.vue b/web/pages/billing/index.vuenew file mode 100644index 00000000..d9f4380e--- /dev/null+++ b/web/pages/billing/index.vue@@ -0,0 +1,203 @@+<template>+ <v-container fluid class="pa-0" :fill-height="$vuetify.breakpoint.lgAndUp">+ <div class="w-full h-full">+ <v-app-bar height="60" :dense="$vuetify.breakpoint.mdAndDown">+ <v-btn icon to="/">+ <v-icon>{{ mdiArrowLeft }}</v-icon>+ </v-btn>+ <v-toolbar-title>+ <div class="py-16">Account Usage</div>+ </v-toolbar-title>+ <v-progress-linear+ :active="loading"+ :indeterminate="loading"+ absolute+ bottom+ ></v-progress-linear>+ </v-app-bar>+ <v-container>+ <v-row>+ <v-col cols="12" md="9" offset-md="1" xl="8" offset-xl="2">+ <h5 class="text-h4 mb-3 mt-3">Overview</h5>+ <p class="text--secondary">+ This is the summary of the sent messages and received messages in+ <code+ v-if="$store.getters.getBillingUsage"+ class="font-weight-bold"+ >{{+ $store.getters.getBillingUsage.start_timestamp | billingPeriod+ }}</code+ >.+ </p>+ <v-row v-if="$store.getters.getBillingUsage">+ <v-col cols="12" md="4">+ <v-alert+ dark+ dense+ :icon="mdiCallMade"+ prominent+ type="info"+ text+ >+ <h2 class="text-h4 font-weight-bold mt-4">+ {{ $store.getters.getBillingUsage.sent_messages | decimal }}+ </h2>+ <p class="text--secondary mt-n1">Messages Sent</p>+ </v-alert>+ </v-col>+ <v-col cols="12" md="4">+ <v-alert+ dark+ dense+ :icon="mdiCallReceived"+ prominent+ type="warning"+ text+ >+ <div class="d-flex">+ <h2 class="text-h4 font-weight-bold mt-4">+ {{+ $store.getters.getBillingUsage.received_messages+ | decimal+ }}+ </h2>+ </div>+ <p class="text--secondary mt-n1">Messages Received</p>+ </v-alert>+ </v-col>+ <v-col cols="12" md="4">+ <v-alert+ dense+ :icon="mdiCreditCard"+ prominent+ type="success"+ text+ >+ <h2 class="text-h4 font-weight-bold mt-4">+ {{ $store.getters.getBillingUsage.total_cost | money }}+ </h2>+ <p class="text--secondary mt-n1">Total Cost</p>+ </v-alert>+ </v-col>+ </v-row>+ <h5 class="text-h4 mb-3 mt-12">Usage History</h5>+ <p class="text--secondary">+ Summary of all the sent and received messages in the past 12+ months+ </p>+ <v-simple-table>+ <template #default>+ <thead>+ <tr class="text-uppercase">+ <th class="text-left">Period</th>+ <th class="text-left">+ Sent+ <span v-if="$vuetify.breakpoint.lgAndUp">Messages</span>+ </th>+ <th class="text-left">+ Received+ <span v-if="$vuetify.breakpoint.lgAndUp">Messages</span>+ </th>+ <th class="text-right">+ <span v-if="$vuetify.breakpoint.lgAndUp">Total</span> Cost+ </th>+ </tr>+ </thead>+ <tbody>+ <tr+ v-for="billingUsage in $store.getters+ .getBillingUsageHistory"+ :key="billingUsage.id"+ >+ <td>+ {{ billingUsage.start_timestamp | billingPeriod }}+ </td>+ <td>+ {{ billingUsage.sent_messages | decimal }}+ </td>+ <td>+ {{ billingUsage.received_messages }}+ </td>+ <td class="text-right font-weight-bold">+ {{ billingUsage.total_cost | money }}+ </td>+ </tr>+ </tbody>+ </template>+ </v-simple-table>+ </v-col>+ </v-row>+ </v-container>+ </div>+ </v-container>+</template>++<script lang="ts">+import Vue from 'vue'+import {+ mdiArrowLeft,+ mdiAccountCircle,+ mdiShieldCheck,+ mdiDelete,+ mdiContentSave,+ mdiEye,+ mdiEyeOff,+ mdiCallReceived,+ mdiCallMade,+ mdiCreditCard,+ mdiSquareEditOutline,+} from '@mdi/js'++export default Vue.extend({+ name: 'BillingIndex',+ middleware: ['auth'],+ data() {+ return {+ mdiEye,+ mdiEyeOff,+ mdiArrowLeft,+ mdiAccountCircle,+ mdiShieldCheck,+ mdiDelete,+ mdiContentSave,+ mdiCallReceived,+ mdiCallMade,+ mdiCreditCard,+ mdiSquareEditOutline,+ loading: true,+ }+ },+ head() {+ return {+ title: 'Usage & Billing - Http SMS',+ }+ },+ computed: {+ apiKey() {+ if (this.$store.getters.getUser === null) {+ return ''+ }+ return this.$store.getters.getUser.api_key+ },+ },+ async mounted() {+ if (!this.$store.getters.getAuthUser) {+ await this.$store.dispatch('setNextRoute', this.$route.path)+ await this.$router.push({ name: 'index' })+ setTimeout(this.loadData, 2000)+ return+ }+ await this.loadData()+ },++ methods: {+ async loadData() {+ await Promise.all([+ this.$store.dispatch('loadBillingUsage'),+ this.$store.dispatch('loadBillingUsageHistory'),+ ])+ this.loading = false+ },+ },+})+</script>diff --git a/web/plugins/filters.ts b/web/plugins/filters.tsindex a15ac124..8e135e30 100644--- a/web/plugins/filters.ts+++ b/web/plugins/filters.ts@@ -25,3 +25,25 @@ Vue.filter('phoneCountry', (value: string): string => {Vue.filter('timestamp', (value: string): string => {return new Date(value).toLocaleString()})++Vue.filter('money', (value: string): string => {+ return new Intl.NumberFormat('en-US', {+ style: 'currency',+ currency: 'USD',+ }).format(parseInt(value))+})++Vue.filter('decimal', (value: string): string => {+ return new Intl.NumberFormat('en-US', {+ style: 'decimal',+ }).format(parseInt(value))+})++Vue.filter('billingPeriod', (value: string): string => {+ const options = {+ year: 'numeric',+ month: 'long',+ }+ // @ts-ignore+ return new Date(value).toLocaleDateString('en-US', options)+})diff --git a/web/store/index.ts b/web/store/index.tsindex b4e5820e..6c1aec89 100644--- a/web/store/index.ts+++ b/web/store/index.ts@@ -6,6 +6,7 @@ import { Heartbeat } from '~/models/heartbeat'import axios from '~/plugins/axios'import { Phone } from '~/models/phone'import { User } from '~/models/user'+import { BillingUsage } from '~/models/billing'const defaultNotificationTimeout = 3000@@ -35,6 +36,8 @@ export type State = {loadingMessages: booleanarchivedThreads: booleanauthUser: AuthUser | null+ billingUsage: BillingUsage | null+ billingUsageHistory: Array<BillingUsage>user: User | nullphones: Array<Phone>threads: Array<MessageThread>@@ -52,6 +55,8 @@ export const state = (): State => ({nextRoute: null,axiosError: null,loadingThreads: true,+ billingUsage: null,+ billingUsageHistory: [],archivedThreads: false,loadingMessages: true,pooling: false,@@ -117,6 +122,14 @@ export const getters = {return state.user},+ getBillingUsageHistory(state: State): Array<BillingUsage> {+ return state.billingUsageHistory+ },++ getBillingUsage(state: State): BillingUsage | null {+ return state.billingUsage+ },+getOwner(state: State): string | null {return state.owner},@@ -186,6 +199,12 @@ export const mutations = {setThreadId(state: State, payload: string | null) {state.threadId = payload},+ setBillingUsageHistory(state: State, payload: Array<BillingUsage>) {+ state.billingUsageHistory = payload+ },+ setBillingUsage(state: State, payload: BillingUsage | null) {+ state.billingUsage = payload+ },setThreadMessages(state: State, payload: Array<Message>) {state.threadMessages = payloadstate.loadingMessages = false@@ -242,6 +261,8 @@ export const mutations = {resetState(state: State) {state.threads = []+ state.billingUsage = null+ state.billingUsageHistory = []state.phones = []state.user = nullstate.threadId = null@@ -282,6 +303,16 @@ export const actions = {await context.commit('setThreads', response.data.data)},+ async loadBillingUsage(context: ActionContext<State, State>) {+ const response = await axios.get('/v1/billing/usage')+ await context.commit('setBillingUsage', response.data.data)+ },++ async loadBillingUsageHistory(context: ActionContext<State, State>) {+ const response = await axios.get('/v1/billing/usage-history')+ await context.commit('setBillingUsageHistory', response.data.data)+ },+async toggleArchive(context: ActionContext<State, State>) {await context.commit('setArchivedThreads', !context.getters.getIsArchived)},