Skip to content

Commit

Permalink
Merge pull request #4633 from mozilla/MNTOR-3271
Browse files Browse the repository at this point in the history
MNTOR-3271: add table and util functions for subscriber coupon code
  • Loading branch information
mansaj authored Jun 6, 2024
2 parents 76be91d + 3952ce5 commit 4e4f2ed
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/db/migrations/20240604053111_subscriber_coupons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

export async function up(knex) {
return knex.schema
.createTable("subscriber_coupons", table => {
table.increments('id').primary()
table.integer("subscriber_id").references("subscribers.id").notNullable().onDelete("CASCADE").onUpdate("CASCADE");
table.string("coupon_code").notNullable()
table.timestamp("created_at").defaultTo(knex.fn.now())
table.unique(["subscriber_id", "coupon_code"])
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export async function down(knex) {
return knex.schema
.dropTableIfExists("subscriber_coupons")
}
52 changes: 52 additions & 0 deletions src/db/tables/subscriber_coupons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import createDbConnection from "../connect.js";
import { logger } from "../../app/functions/server/logging";
import { SubscriberCouponRow } from "knex/types/tables";

const knex = createDbConnection();

async function checkCouponForSubscriber(
subscriberId: number,
couponCode: string,
) {
logger.info("checkCouponForSubscriber", subscriberId);
return !!(await knex("subscriber_coupons")
.where({
subscriber_id: subscriberId,
coupon_code: couponCode,
})
.first());
}

async function addCouponForSubscriber(
subscriberId: number,
couponCode: string,
) {
logger.info("addCouponForSubscriber", { subscriberId, couponCode });

let res;
try {
res = await knex("subscribers_coupon")
.insert({
subscriber_id: subscriberId,
coupon_code: couponCode,
})
.returning("*");
} catch (e) {
if ((e as Error).message.includes("violates unique constraint")) {
logger.error("could_not_add_coupon", {
subscriberId,
error: (e as Error).message,
});
} else {
logger.error("could_not_add_coupon", { error: JSON.stringify(e) });
}
throw e;
}
return res?.[0] as SubscriberCouponRow;
}

export { checkCouponForSubscriber, addCouponForSubscriber };
19 changes: 19 additions & 0 deletions src/knex-tables.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ declare module "knex/types/tables" {
"id" | "created_at" | "updated_at"
>;

interface SubscriberCouponRow {
id: number;
subscriber_id: number;
coupon_code: string;
created_at: Date;
}
type SubscriberCouponAutoInsertedColumns = Extract<
keyof SubscriberCouponRow,
"id" | "subscriber_id" | "created_at"
>;

interface BreachRow {
id: number;
name: string;
Expand Down Expand Up @@ -337,6 +348,14 @@ declare module "knex/types/tables" {
Pick<SubscriberRow, "updated_at">
>;

subscriber_coupons: Knex.CompositeTableType<
SubscriberCouponRow,
// On updates, auto-generated columns cannot be set, and nullable columns are optional:
Omit<SubscriberCouponRow, SubscriberAutoInsertedColumns>,
// On updates, don't allow updating the ID; all other fields are optional:
Partial<Omit<SubscriberCouponRow, "id">>
>;

email_addresses: Knex.CompositeTableType<
EmailAddressRow,
// On updates, auto-generated columns cannot be set, and nullable columns are optional:
Expand Down

0 comments on commit 4e4f2ed

Please sign in to comment.