Skip to content

add exchanging of merch #105

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

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1af8f08
exchange route
jlevine18 Apr 2, 2025
3ddc3a9
Merge branch 'main' into merch-exchanges
jlevine18 Apr 2, 2025
59ca93f
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 15, 2025
0d78a31
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 15, 2025
52e763e
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 19, 2025
98797f4
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 20, 2025
759dde2
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 20, 2025
c83d796
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 22, 2025
b5d2e42
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 22, 2025
bef04d9
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 22, 2025
cf2a058
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 23, 2025
69daa44
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 23, 2025
553b932
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 23, 2025
97d7b0b
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 23, 2025
1039826
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 23, 2025
831f5f2
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 24, 2025
8d299b2
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 24, 2025
770b179
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 25, 2025
6554b57
Auto-update feature branch with changes from the main branch
github-actions[bot] Jun 27, 2025
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
76 changes: 76 additions & 0 deletions src/api/routes/tickets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
QueryCommand,
ScanCommand,
UpdateItemCommand,
TransactWriteItemsCommand,
TransactWriteItemsInput,
} from "@aws-sdk/client-dynamodb";
import { genericConfig } from "../../common/config.js";
import {
Expand Down Expand Up @@ -94,6 +96,21 @@ const postSchema = z.union([postMerchSchema, postTicketSchema]);

type VerifyPostRequest = z.infer<typeof postSchema>;

const itemExchangeSchema = z.object({
itemId: z.string().min(1),
ticketId: z.string().min(1),
originalSize: z.string(),
newSize: z.string(),
quantityExchanged: z.number(),
});

type ItemExchangeRequest = z.infer<typeof itemExchangeSchema>;

const itemExchangeResponseSchema = z.object({
ticketId: z.string().min(1),
successful: z.boolean(),
});

type TicketsGetRequest = {
Params: { id: string };
Querystring: { type: string };
Expand Down Expand Up @@ -477,6 +494,65 @@ const ticketsPlugin: FastifyPluginAsync = async (fastify, _options) => {
});
},
);
fastify.post<{ Body: ItemExchangeRequest }>(
"/exchange",
{
schema: {
response: { 200: itemExchangeResponseSchema },
},
preValidation: async (request, reply) => {
await fastify.zodValidateBody(request, reply, itemExchangeSchema);
},
onRequest: async (request, reply) => {
await fastify.authorize(request, reply, [AppRoles.TICKETS_SCANNER]);
},
},
async (request, reply) => {
const transaction: TransactWriteItemsInput = {
TransactItems: [
{
Update: {
Key: { itemId: { S: request.body.itemId } },
UpdateExpression:
"SET total_avail.#sizeold = total_avail.#sizeold + :quantity, total_avail.#sizenew = total_avail.#sizenew + :quantity",
ConditionExpression: "total_avail.#sizenew >= :quantity",
ExpressionAttributeNames: {
"#sizeold": request.body.originalSize,
"#sizenew": request.body.newSize,
},
ExpressionAttributeValues: {
":quantity": { N: request.body.quantityExchanged.toString() },
},
TableName: genericConfig.MerchStoreMetadataTableName,
},
},
{
Update: {
Key: { stripePi: { S: request.body.ticketId } },
UpdateExpression: "SET size = :sizenew",
ConditionExpression: "quantity >= :quantity",
ExpressionAttributeValues: {
":quantity": { N: request.body.quantityExchanged.toString() },
":sizenew": { S: request.body.newSize },
},
TableName: genericConfig.MerchStorePurchasesTableName,
},
},
],
};
const command = new TransactWriteItemsCommand(transaction);
try {
const resp = await fastify.dynamoClient.send(command);
if (resp.$metadata.httpStatusCode == 200) {
reply.send({ ticketId: request.body.ticketId, successful: true });
} else {
reply.send({ ticketId: request.body.ticketId, successful: false });
}
} catch {
reply.send({ ticketId: request.body.ticketId, successful: false });
}
},
);
};

export default ticketsPlugin;