-
Notifications
You must be signed in to change notification settings - Fork 220
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 #4633 from mozilla/MNTOR-3271
MNTOR-3271: add table and util functions for subscriber coupon code
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
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,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") | ||
} |
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,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 }; |
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