Add mailer API and initial mailer view
This commit is contained in:
parent
0d8d7f3aeb
commit
eb27af7d90
10 changed files with 479 additions and 0 deletions
|
|
@ -38,6 +38,9 @@ func AddRoutes(r *mux.Router) {
|
|||
lead := resource.Lead(r)
|
||||
r.Handle("/leads", authenticatedHandlerJSON(lead.List)).Methods("GET")
|
||||
r.Handle("/leads", authenticatedHandlerJSONPost(lead.Create)).Methods("POST")
|
||||
mailer := resource.Mailer(router)
|
||||
r.Handle("/mailer", authenticatedHandlerJSONSlice(mailer.List)).Methods("GET")
|
||||
r.Handle("/mailer/{id}", authenticatedHandlerJSONPost(mailer.ByIDGet)).Methods("GET").Name("mailer.ByIDGet")
|
||||
r.Handle("/mosquito-source", auth.NewEnsureAuth(apiMosquitoSource)).Methods("GET")
|
||||
|
||||
r.Handle("/publicreport/invalid", authenticatedHandlerJSONPost(postPublicreportInvalid)).Methods("POST")
|
||||
|
|
|
|||
88
platform/mailer.go
Normal file
88
platform/mailer.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
"github.com/stephenafamo/scan"
|
||||
)
|
||||
|
||||
func MailerByID(ctx context.Context, user User, id int32) (*types.Mailer, error) {
|
||||
query := mailerQuery()
|
||||
query.Apply(
|
||||
sm.Where(models.ComplianceReportRequests.Columns.ID.EQ(psql.Arg(id))),
|
||||
sm.Where(
|
||||
models.Sites.Columns.OrganizationID.EQ(psql.Arg(user.Organization.ID)),
|
||||
),
|
||||
)
|
||||
mailers, err := mailerQueryToRows(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mailers[id], nil
|
||||
}
|
||||
func MailerList(ctx context.Context, user User, limit int) ([]*types.Mailer, error) {
|
||||
query := mailerQuery()
|
||||
query.Apply(
|
||||
sm.Where(
|
||||
models.Sites.Columns.OrganizationID.EQ(psql.Arg(user.Organization.ID)),
|
||||
),
|
||||
sm.OrderBy(models.ComplianceReportRequests.Columns.Created),
|
||||
sm.Limit(limit),
|
||||
)
|
||||
return mailerQueryToRows(ctx, query)
|
||||
}
|
||||
func mailerQuery() bob.BaseQuery[*dialect.SelectQuery] {
|
||||
return psql.Select(
|
||||
sm.Columns(
|
||||
models.Addresses.Columns.Country.As("address.country"),
|
||||
models.Addresses.Columns.Locality.As("address.locality"),
|
||||
//sm.From(psql.F("COALESCE", psql.Raw("address.location_latitude"), 0)).As("address.location.latitude"),
|
||||
//sm.From(psql.F("COALESCE", psql.Raw("address.location_longitude"), 0)).As("address.location.longitude"),
|
||||
"COALESCE(address.location_latitude, 0) AS \"address.location.latitude\"",
|
||||
"COALESCE(address.location_longitude, 0) AS \"address.location.longitude\"",
|
||||
models.Addresses.Columns.Number.As("address.number"),
|
||||
models.Addresses.Columns.PostalCode.As("address.postal_code"),
|
||||
models.Addresses.Columns.Region.As("address.region"),
|
||||
models.Addresses.Columns.Street.As("address.street"),
|
||||
models.Addresses.Columns.Unit.As("address.unit"),
|
||||
models.ComplianceReportRequests.Columns.Created.As("created"),
|
||||
models.ComplianceReportRequests.Columns.PublicID.As("compliance_report_request_id"),
|
||||
models.Sites.Columns.ID.As("site_id"),
|
||||
models.Sites.Columns.OwnerName.As("recipient"),
|
||||
"'created' AS \"status\"",
|
||||
),
|
||||
sm.From(models.ComplianceReportRequestMailers.Name()),
|
||||
sm.InnerJoin(models.ComplianceReportRequests.Name()).OnEQ(
|
||||
models.ComplianceReportRequestMailers.Columns.ComplianceReportRequestID,
|
||||
models.ComplianceReportRequests.Columns.ID,
|
||||
),
|
||||
sm.InnerJoin(models.Leads.Name()).OnEQ(
|
||||
models.ComplianceReportRequests.Columns.LeadID,
|
||||
models.Leads.Columns.ID,
|
||||
),
|
||||
sm.InnerJoin(models.Sites.Name()).OnEQ(
|
||||
models.Leads.Columns.SiteID,
|
||||
models.Sites.Columns.ID,
|
||||
),
|
||||
sm.InnerJoin(models.Addresses.Name()).OnEQ(
|
||||
models.Sites.Columns.AddressID,
|
||||
models.Addresses.Columns.ID,
|
||||
),
|
||||
)
|
||||
}
|
||||
func mailerQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.Mailer, error) {
|
||||
rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[*types.Mailer]())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query mailers: %w", err)
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
16
platform/types/mailer.go
Normal file
16
platform/types/mailer.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Mailer struct {
|
||||
Address Address `json:"address"`
|
||||
ComplianceReportRequestID *string `json:"compliance_report_request_id"`
|
||||
Created time.Time `json:"created"`
|
||||
ID int32 `json:"id"`
|
||||
Recipient string `json:"recipient"`
|
||||
SiteID int32 `json:"site_id"`
|
||||
Status string `json:"status"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
55
resource/mailer.go
Normal file
55
resource/mailer.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
//"github.com/aarondl/opt/null"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type mailerR struct {
|
||||
router *router
|
||||
}
|
||||
|
||||
func Mailer(r *router) *mailerR {
|
||||
return &mailerR{
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *mailerR) ByIDGet(ctx context.Context, r *http.Request, user platform.User, query QueryParams) (*types.Mailer, *nhttp.ErrorWithStatus) {
|
||||
vars := mux.Vars(r)
|
||||
id_str := vars["id"]
|
||||
id, err := strconv.Atoi(id_str)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewBadRequest("'%s' is not a valid mailer ID: %w", id_str, err)
|
||||
}
|
||||
mailer, err := platform.MailerByID(ctx, user, int32(id))
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("mailer by id: %w", err)
|
||||
}
|
||||
return mailer, nil
|
||||
}
|
||||
func (res *mailerR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) ([]*types.Mailer, *nhttp.ErrorWithStatus) {
|
||||
limit := 1000
|
||||
if query.Limit != nil {
|
||||
limit = *query.Limit
|
||||
}
|
||||
mailers, err := platform.MailerList(ctx, user, limit)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("list signals: %w", err)
|
||||
}
|
||||
for _, mailer := range mailers {
|
||||
uri, err := res.router.IDToURI("mailer.ByIDGet", int(mailer.ID))
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("set uri: %w", err)
|
||||
}
|
||||
mailer.URI = uri
|
||||
}
|
||||
return mailers, nil
|
||||
}
|
||||
|
|
@ -50,6 +50,7 @@ type sessionURLAPI struct {
|
|||
Avatar string `json:"avatar"`
|
||||
Communication string `json:"communication"`
|
||||
Impersonation string `json:"impersonation"`
|
||||
Mailer string `json:"mailer"`
|
||||
PublicreportMessage string `json:"publicreport_message"`
|
||||
ReviewTask string `json:"review_task"`
|
||||
ServiceRequest string `json:"service_request"`
|
||||
|
|
@ -98,6 +99,7 @@ func (res *sessionR) Get(ctx context.Context, r *http.Request, user platform.Use
|
|||
Avatar: config.MakeURLNidus("/api/avatar"),
|
||||
Communication: urls.API.Communication,
|
||||
Impersonation: config.MakeURLNidus("/api/impersonation"),
|
||||
Mailer: config.MakeURLNidus("/api/mailer"),
|
||||
PublicreportMessage: urls.API.Publicreport.Message,
|
||||
ReviewTask: config.MakeURLNidus("/api/review-task"),
|
||||
ServiceRequest: config.MakeURLNidus("/api/service-request"),
|
||||
|
|
|
|||
10
ts/format.ts
10
ts/format.ts
|
|
@ -24,6 +24,16 @@ export function formatBigNumber(n: number): string {
|
|||
|
||||
return result;
|
||||
}
|
||||
export function formatDate(date: Date): string {
|
||||
return new Intl.DateTimeFormat("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
export function formatDistance(meters: number | undefined) {
|
||||
if (meters === undefined || meters === null) {
|
||||
return "unknown";
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import NotFound from "@/view/NotFound.vue";
|
|||
import OAuthRefreshArcgis from "@/view/OAuthRefreshArcgis.vue";
|
||||
import Operations from "@/view/Operations.vue";
|
||||
import Planning from "@/view/Planning.vue";
|
||||
import ReviewMailer from "@/view/review/Mailer.vue";
|
||||
import ReviewPool from "@/view/review/Pool.vue";
|
||||
import ReviewRoot from "@/view/review/Root.vue";
|
||||
import ReviewSite from "@/view/review/Site.vue";
|
||||
|
|
@ -153,6 +154,11 @@ const routes: RouteRecordRaw[] = [
|
|||
name: "Review",
|
||||
component: ReviewRoot,
|
||||
},
|
||||
{
|
||||
path: "/_/review/mailer",
|
||||
name: "Mailer Review",
|
||||
component: ReviewMailer,
|
||||
},
|
||||
{
|
||||
path: "/_/review/pool",
|
||||
name: "Pool Review",
|
||||
|
|
|
|||
85
ts/store/mailer.ts
Normal file
85
ts/store/mailer.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { useSessionStore } from "@/store/session";
|
||||
|
||||
import { apiClient } from "@/client";
|
||||
import { Mailer, type MailerDTO } from "@/type/api";
|
||||
|
||||
export const useStoreMailer = defineStore("publicreport", () => {
|
||||
// State
|
||||
const _all = ref<Mailer[] | null>(null);
|
||||
const _byID = ref<Map<string, Mailer>>(new Map());
|
||||
const loading = ref(false);
|
||||
const ongoingFetch = ref<Promise<Mailer[]> | null>(null);
|
||||
|
||||
// Actions
|
||||
async function byID(id: string): Promise<Mailer> {
|
||||
const r = _byID.value.get(id);
|
||||
if (r) {
|
||||
return r;
|
||||
}
|
||||
return fetchByID(id);
|
||||
}
|
||||
async function byURI(uri: string): Promise<Mailer> {
|
||||
const id = uri.split("/").pop() || "";
|
||||
if (!id) {
|
||||
throw new Error(`${uri} is not a recognized public report URI`);
|
||||
}
|
||||
return byID(id);
|
||||
}
|
||||
async function fetchAll(): Promise<Mailer[]> {
|
||||
const sessionStore = useSessionStore();
|
||||
const session = await sessionStore.get();
|
||||
loading.value = true;
|
||||
const params = new URLSearchParams();
|
||||
params.append("sort", "-created");
|
||||
const url = `${session.urls.api.mailer}?${params}`;
|
||||
const mailers = (await apiClient.JSONGet(url)) as Mailer[];
|
||||
_all.value = mailers;
|
||||
for (const m of mailers) {
|
||||
_byID.value.set(m.id, m);
|
||||
}
|
||||
return mailers;
|
||||
}
|
||||
async function fetchByID(id: string): Promise<Mailer> {
|
||||
const uri = `/api/publicreport/${id}`;
|
||||
return fetchByURI(uri);
|
||||
}
|
||||
async function fetchByURI(uri: string): Promise<Mailer> {
|
||||
loading.value = true;
|
||||
try {
|
||||
const response = await fetch(uri);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const body: MailerDTO = await response.json();
|
||||
const report = Mailer.fromJSON(body);
|
||||
_byID.value.set(report.id, report);
|
||||
return report;
|
||||
} catch (err) {
|
||||
console.error("Error loading users:", err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
async function list(): Promise<Mailer[]> {
|
||||
if (_all.value) {
|
||||
return _all.value;
|
||||
}
|
||||
if (ongoingFetch.value !== null) {
|
||||
return ongoingFetch.value;
|
||||
}
|
||||
ongoingFetch.value = fetchAll().finally(() => {
|
||||
ongoingFetch.value = null;
|
||||
});
|
||||
return ongoingFetch.value;
|
||||
}
|
||||
return {
|
||||
// Actions
|
||||
byID,
|
||||
byURI,
|
||||
fetchByID,
|
||||
fetchByURI,
|
||||
list,
|
||||
};
|
||||
});
|
||||
|
|
@ -659,6 +659,54 @@ export interface User {
|
|||
uri: string;
|
||||
username: string;
|
||||
}
|
||||
type MailerStatus = "created" | "printed" | "mailed" | "completed";
|
||||
export interface MailerDTO {
|
||||
address: Address;
|
||||
compliance_report_request_id?: string;
|
||||
created: string;
|
||||
id: string;
|
||||
recipient: string;
|
||||
status: MailerStatus;
|
||||
site_id: string;
|
||||
uri: string;
|
||||
}
|
||||
export interface MailerOptions {
|
||||
address: Address;
|
||||
compliance_report_request_id?: string;
|
||||
created: Date;
|
||||
id: string;
|
||||
recipient: string;
|
||||
site_id: string;
|
||||
status: MailerStatus;
|
||||
uri: string;
|
||||
}
|
||||
export class Mailer {
|
||||
address: Address;
|
||||
compliance_report_request_id?: string;
|
||||
created: Date;
|
||||
id: string;
|
||||
recipient: string;
|
||||
site_id: string;
|
||||
status: MailerStatus;
|
||||
uri: string;
|
||||
constructor(options: MailerOptions) {
|
||||
this.address = options.address;
|
||||
this.compliance_report_request_id = options.compliance_report_request_id;
|
||||
this.created = options.created;
|
||||
this.id = options.id;
|
||||
this.recipient = options.recipient;
|
||||
this.site_id = options.site_id;
|
||||
this.status = options.status;
|
||||
this.uri = options.uri;
|
||||
}
|
||||
static fromJSON(json: MailerDTO): Mailer {
|
||||
return new Mailer({
|
||||
...json,
|
||||
created: new Date(json.created),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export interface Organization {
|
||||
id: number;
|
||||
name: string;
|
||||
|
|
@ -791,6 +839,7 @@ interface URLsAPI {
|
|||
avatar: string;
|
||||
communication: string;
|
||||
impersonation: string;
|
||||
mailer: string;
|
||||
publicreport_message: string;
|
||||
review_task: string;
|
||||
service_request: string;
|
||||
|
|
|
|||
165
ts/view/review/Mailer.vue
Normal file
165
ts/view/review/Mailer.vue
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
<style scoped>
|
||||
.table th {
|
||||
font-weight: 600;
|
||||
border-bottom: 2px solid #dee2e6;
|
||||
}
|
||||
|
||||
.badge {
|
||||
font-weight: 500;
|
||||
padding: 0.375rem 0.75rem;
|
||||
}
|
||||
</style>
|
||||
<template>
|
||||
<div class="container-fluid py-4">
|
||||
<div class="row mb-4">
|
||||
<div class="col">
|
||||
<h1 class="h2">Mailers</h1>
|
||||
<p class="text-muted">Track the status of your postal mailers</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Created</th>
|
||||
<th>Status</th>
|
||||
<th>Site Address</th>
|
||||
<th>PDF</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="mailer in mailers" :key="mailer.id">
|
||||
<td>{{ formatDate(mailer.createdAt) }}</td>
|
||||
<td>
|
||||
<span
|
||||
class="badge"
|
||||
:class="getStatusBadgeClass(mailer.status)"
|
||||
>
|
||||
{{ formatStatus(mailer.status) }}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
<a :href="`/sites/${mailer.siteId}`">
|
||||
{{ mailer.siteAddress }}
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<a
|
||||
:href="mailer.pdfUrl"
|
||||
target="_blank"
|
||||
class="btn btn-sm btn-outline-primary"
|
||||
>
|
||||
<i class="bi bi-file-pdf"></i> View PDF
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="mailers.length === 0"
|
||||
class="text-center py-5 text-muted"
|
||||
>
|
||||
<p>No mailers found</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
|
||||
import { useStoreMailer } from "@/store/mailer";
|
||||
|
||||
type MailerStatus = "created" | "printed" | "mailed" | "completed";
|
||||
|
||||
interface Mailer {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
status: MailerStatus;
|
||||
pdfUrl: string;
|
||||
siteId: string;
|
||||
siteAddress: string;
|
||||
}
|
||||
|
||||
// Mock data
|
||||
const mailers = ref<Mailer[]>([
|
||||
{
|
||||
id: "1",
|
||||
createdAt: new Date("2024-01-15T10:30:00"),
|
||||
status: "completed",
|
||||
pdfUrl: "/pdfs/mailer-1.pdf",
|
||||
siteId: "101",
|
||||
siteAddress: "123 Main St, Springfield, IL 62701",
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
createdAt: new Date("2024-01-16T14:20:00"),
|
||||
status: "mailed",
|
||||
pdfUrl: "/pdfs/mailer-2.pdf",
|
||||
siteId: "102",
|
||||
siteAddress: "456 Oak Ave, Chicago, IL 60601",
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
createdAt: new Date("2024-01-17T09:15:00"),
|
||||
status: "printed",
|
||||
pdfUrl: "/pdfs/mailer-3.pdf",
|
||||
siteId: "103",
|
||||
siteAddress: "789 Pine Rd, Naperville, IL 60540",
|
||||
},
|
||||
{
|
||||
id: "4",
|
||||
createdAt: new Date("2024-01-18T11:45:00"),
|
||||
status: "created",
|
||||
pdfUrl: "/pdfs/mailer-4.pdf",
|
||||
siteId: "104",
|
||||
siteAddress: "321 Elm St, Peoria, IL 61602",
|
||||
},
|
||||
{
|
||||
id: "5",
|
||||
createdAt: new Date("2024-01-18T16:00:00"),
|
||||
status: "mailed",
|
||||
pdfUrl: "/pdfs/mailer-5.pdf",
|
||||
siteId: "105",
|
||||
siteAddress: "654 Maple Dr, Rockford, IL 61101",
|
||||
},
|
||||
]);
|
||||
|
||||
const formatDate = (date: Date): string => {
|
||||
return new Intl.DateTimeFormat("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
}).format(date);
|
||||
};
|
||||
|
||||
const formatStatus = (status: MailerStatus): string => {
|
||||
return status.charAt(0).toUpperCase() + status.slice(1);
|
||||
};
|
||||
|
||||
const getStatusBadgeClass = (status: MailerStatus): string => {
|
||||
const classes: Record<MailerStatus, string> = {
|
||||
created: "bg-secondary",
|
||||
printed: "bg-info",
|
||||
mailed: "bg-primary",
|
||||
completed: "bg-success",
|
||||
};
|
||||
return classes[status];
|
||||
};
|
||||
const storeMailer = useStoreMailer();
|
||||
onMounted(() => {
|
||||
storeMailer.list();
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue