This repository has been archived by the owner on Aug 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from beabee-communityrm/feat/payment-api
Add a basic read only payment API
- Loading branch information
Showing
8 changed files
with
133 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Paginated, fetchPaginated, mergeRules } from "@api/data/PaginatedData"; | ||
import { | ||
Authorized, | ||
CurrentUser, | ||
Get, | ||
JsonController, | ||
QueryParams | ||
} from "routing-controllers"; | ||
|
||
import { | ||
GetPaymentData, | ||
GetPaymentsQuery, | ||
fetchPaginatedPayments | ||
} from "@api/data/PaymentData"; | ||
|
||
import Contact from "@models/Contact"; | ||
|
||
@JsonController("/payment") | ||
@Authorized() | ||
export class PaymentController { | ||
@Get("/") | ||
async getPayments( | ||
@CurrentUser() contact: Contact, | ||
@QueryParams() query: GetPaymentsQuery | ||
): Promise<Paginated<GetPaymentData>> { | ||
const authedQuery = { | ||
...query, | ||
rules: mergeRules([ | ||
query.rules, | ||
// Non-admins can only see their own payments | ||
!contact.hasRole("admin") && { | ||
field: "contact", | ||
operator: "equal", | ||
value: [contact.id] | ||
} | ||
]) | ||
}; | ||
return await fetchPaginatedPayments(authedQuery, contact); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Payment from "@models/Payment"; | ||
import { GetPaymentData, GetPaymentWith, GetPaymentsQuery } from "./interface"; | ||
import { Paginated, paymentFilters } from "@beabee/beabee-common"; | ||
import { fetchPaginated } from "../PaginatedData"; | ||
import Contact from "@models/Contact"; | ||
import { convertContactToData, loadContactRoles } from "../ContactData"; | ||
|
||
function convertPaymentToPaymentData( | ||
payment: Payment, | ||
_with?: GetPaymentWith[] | ||
): GetPaymentData { | ||
return { | ||
amount: payment.amount, | ||
chargeDate: payment.chargeDate, | ||
status: payment.status, | ||
...(_with?.includes(GetPaymentWith.Contact) && { | ||
contact: payment.contact && convertContactToData(payment.contact) | ||
}) | ||
}; | ||
} | ||
|
||
export async function fetchPaginatedPayments( | ||
query: GetPaymentsQuery, | ||
contact: Contact | ||
): Promise<Paginated<GetPaymentData>> { | ||
const data = await fetchPaginated( | ||
Payment, | ||
paymentFilters, | ||
query, | ||
contact, | ||
undefined, | ||
(qb, fieldPrefix) => { | ||
if (query.with?.includes(GetPaymentWith.Contact)) { | ||
qb.leftJoinAndSelect(`${fieldPrefix}contact`, "contact"); | ||
} | ||
} | ||
); | ||
|
||
// Load contact roles after to ensure offset/limit work | ||
const contacts = data.items | ||
.map((item) => item.contact) | ||
.filter((c) => !!c) as Contact[]; | ||
await loadContactRoles(contacts); | ||
|
||
return { | ||
...data, | ||
items: data.items.map((item) => | ||
convertPaymentToPaymentData(item, query.with) | ||
) | ||
}; | ||
} | ||
|
||
export * from "./interface"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PaymentStatus } from "@beabee/beabee-common"; | ||
import { IsArray, IsEnum, IsIn, IsOptional } from "class-validator"; | ||
|
||
import { GetPaginatedQuery } from "../PaginatedData"; | ||
import { GetContactData } from "../ContactData"; | ||
|
||
export interface GetPaymentData { | ||
amount: number; | ||
chargeDate: Date; | ||
status: PaymentStatus; | ||
contact?: GetContactData | null; | ||
} | ||
|
||
export enum GetPaymentWith { | ||
Contact = "contact" | ||
} | ||
|
||
const paymentSortFields = ["amount", "chargeDate"] as const; | ||
export class GetPaymentsQuery extends GetPaginatedQuery { | ||
@IsArray() | ||
@IsOptional() | ||
@IsEnum(GetPaymentWith, { each: true }) | ||
with?: GetPaymentWith[]; | ||
|
||
@IsIn(paymentSortFields) | ||
sort?: string; | ||
} |