Course Builder: Add a Method to the Adapter

We add a new method called incrementCouponUsedCount to the adapter in the Course Builder core. The core can't directly access the database, so it uses an adapter. We add this new method to the adapter interface to increase the count of a coupon's use, and we adjust the mock setup so it works.

We also discuss how the adapter lets us manage different database actions without direct access from the core. We look at expanding the adapters to work with other databases like MySQL, Postgres, or SQLite.

Finally, we think about making the adapter's structure simpler and how to better handle newer parts like notifications and handling database transactions. This update makes way for more tests in the system's tools.

Transcript

[00:02] All right, so this is just quickly adding a method to the adapter and what I have going on here is inside of core, we're redeeming a golden ticket so that flows through. Course builder internal is basically the router and it picks up the action and then figures out the action and calls that and then there would be providers also. In this case, it doesn't actually matter. We're not switching on the provider. I don't, I'll have to make sure that we can redeem and that this flows through, I haven't done that yet.

[00:34] But anyway, so then that flows into redeem, which is an action inside of core. And the thing about core is that we do not have access to the database directly. So where we were calling Prisma in our other example here is the Skill Stack products example. If you scroll in here we're calling Prisma directly and we don't do that. We use the adapter here and I've done things like get existing non-bulk valid purchases of product and then we're creating purchase through the adapter with the various options passed in and then those get picked up.

[01:18] Here's the interface for the adapter where we have the course builder adapter and it's using this skill products commerce SDK which I exported, which was the API that came from products. So that's our Prisma API here, right? Like it had all these methods. Now we're, we're have that as an interface that we're using to build up over here and use to create various database items and do stuff that we need inside of core without having direct access to the database which is provided via adapters and then those adapters are configured on the consumer side and should be agnostic this has no Core has no knowledge whatsoever of Next.js in particular, so you could build other things and then adapters like this is MySQL but then you could add a Postgres or SQLite or whatever to the drizzle adapter and kind of expand that. It doesn't do that now, it did, I removed it because I wasn't using it and just using MySQL for now but that's something that we can do to expand or even create other adapters so you could have a sanity adapter for instance or whatever it's it's pretty arbitrary.

[02:33] What I wanted to do here was to actually increment the coupon used to count was the method name and it's gonna be a coupon ID and we're gonna take that in and this doesn't return anything it just increments the coupon so I'm going to add that to the interface that instantly makes my little mock up here break And just give it something so it just functions, right? Like that's all that needs to do. Then I'm going to come over here and I want to add to this. So this is like basically humongous, right? Like it has all the stuff.

[03:24] So it's kind of a, being honest, kind of a pain in the ass. I think these could be probably separated into individual little pieces. If at some point we wanted to and then frankly it just kind of works too so that's fine. And do that. I don't know.

[03:54] Yeah, I'd be curious if you can increment with drizzle. Let's see. Increment value. Increment value. So you can add your own increment here.

[04:28] I'm going to do that client update coupon with the coupon ID set. I don't need all this. I think that's it. Nice. One of the things that we did in the original was use a transaction and I haven't done that.

[04:50] Rizzle does have transactions but I'm not using that. Let's see if we have that. So that's not part of it yet. We're going to go ahead and... Come down here, extends that.

[05:16] Options, good. Feels like you'd want to send the purchase transfer back, but I don't care, I'm not going to do it. And then we just come into here. I say, I don't care, I'm not gonna do it. It's just, this is using Super Maven for the completion bits, which I've been enjoying quite a bit.

[05:45] It's kind of nailing stuff. Let's see. Expires at values. Purchase ID is missing in type. Yes, it's not ID, it is purchase ID.

[06:10] And what did I miss? Purchase user transfer, so ID I'm going to do this because I like it. Purchase user transfer. I'll make it put, there we go. And just add that like so, cool.

[06:43] And now, do that and it'll create the user transfer. So then maybe I do like to do use a transaction for this and then the rest of it is actually the notifications right So we're sending email and that sort of stuff which is something that I need to get worked out in the system. Particularly, I don't know if Core does that at all, but that's the kind of end-to-end vibe of creating that. Again, this was mostly adding to core where the adapter is, where the adapter interface is located, and update the mock to, and then adding the actual implementation inside of this and this is something that we can then add tests for really because we have down here in utils there is adapter so it's like it does the incrementor actually work within the context of the adapter? Is something that we can write a test for if we want or need to.