WEBVTT

00:00.110 --> 00:05.480
So we're ready to go inside of our reservation service now and actually make a call to the payment service

00:05.480 --> 00:07.440
and test this functionality out.

00:07.460 --> 00:13.700
Firstly, we're going to go ahead and open up our reservations module or have to go ahead and provide

00:13.700 --> 00:20.570
the newly created payment service to our client module here so that we can inject it and actually communicate

00:20.570 --> 00:21.290
with it.

00:21.290 --> 00:25.610
So we can simply go ahead and copy this auth service object.

00:25.610 --> 00:30.020
And I will change this out now to a new service.

00:30.020 --> 00:32.930
So in our constants folder in lib slash Common.

00:33.800 --> 00:41.330
We have our services file here and we'll go ahead and export a const called Payments Service and this

00:41.330 --> 00:42.740
will be called payments.

00:42.740 --> 00:48.800
So now in our reservations module, let's go ahead and change the injection token out here to payment

00:48.800 --> 00:49.730
service.

00:49.730 --> 00:54.770
We're still going to inject the config service, but now we're going to be looking for the payments

00:54.770 --> 00:57.860
host and the payments port.

00:58.400 --> 01:02.660
So let's go ahead in our dot env and actually set this up.

01:02.660 --> 01:09.530
I'll break things up here and add a new payments host which we know in our Docker compose set up will

01:09.530 --> 01:15.920
be the name of the service that we define in here directly is going to be the host name for that service.

01:15.920 --> 01:18.470
So we know it'll just be payments.

01:18.470 --> 01:22.970
So let's go back into our ENV and set the host here to payments.

01:22.970 --> 01:30.530
And then we have the payments port which if you remember correctly, in our payments service, we defined

01:30.530 --> 01:32.620
this at 3003.

01:32.630 --> 01:35.030
So let's go ahead and set that as well.

01:35.040 --> 01:42.840
Finally in our Reservations module, let's go ahead and actually add some schema validation to all of

01:42.840 --> 01:44.580
our microservices right now.

01:44.580 --> 01:52.740
I'll go ahead and add an auth host, which is a joy dot string dot required and I'll copy this for the

01:52.740 --> 01:57.000
next one, which is the payments host, which is also a required string.

01:57.000 --> 02:04.080
And then we have the auth port, which is a type number and this will be required.

02:04.080 --> 02:08.730
And then lastly, we'll do the same here for the payments port.

02:08.730 --> 02:15.300
So make sure as we update the environment variable that we, we start our Docker containers and allow

02:15.300 --> 02:17.310
that new environment variable to be read in.

02:17.310 --> 02:23.940
So now let's go back into our reservation service and let's go ahead and inject by using the inject

02:23.940 --> 02:24.930
decorator here.

02:24.930 --> 02:31.170
We'll go ahead and provide the payments service as our injection token and we'll go ahead and change

02:31.170 --> 02:37.010
the import here to apps slash common to make sure we import that correctly.

02:37.010 --> 02:38.810
So now we're injecting the payment service.

02:38.810 --> 02:45.350
We'll go ahead and call this payments service of type client proxy here from Nestjs microservices.

02:45.890 --> 02:52.670
So now in our Create method here where we're actually creating our reservations and I'm now just realizing

02:52.670 --> 02:58.790
that we actually need to make sure that all of these functions here are async because these all return

02:58.790 --> 02:59.540
a promise.

02:59.540 --> 03:02.270
So let's go ahead and update these function signatures here.

03:02.990 --> 03:08.210
And now inside of Korea, instead of just creating the reservation, after I create the reservation

03:08.210 --> 03:13.910
in our database, I want to go ahead and reach out to our payment service and bill the appropriate user

03:13.910 --> 03:15.440
for this reservation.

03:15.440 --> 03:21.380
So the first thing we'll have to do is go into our Create Reservation, DTO and update this here so

03:21.380 --> 03:27.410
we know we need to add the credit card in the Create Reservation DTO So let's go ahead and add a new

03:27.410 --> 03:34.550
card property here and we want to add a sub DTO here so that we can apply validation to this actual

03:34.550 --> 03:36.230
card object itself.

03:36.230 --> 03:44.420
So let's go ahead and create a new DTO in the reservations folder and we'll call this card DTO Dots,

03:44.420 --> 03:46.820
and we'll go ahead and export a card.

03:46.850 --> 03:54.710
DTO here now inside of the card, DTO, we can go back into our payment service and confirm the type

03:54.710 --> 04:00.800
on the card here, which we know will be of type stripe payment method, create params card one so we

04:00.800 --> 04:03.840
can see all the properties on the card here that we want to validate.

04:03.840 --> 04:10.140
So let's go ahead and copy these properties and paste them into this card DTO so that we can validate

04:10.140 --> 04:10.710
them.

04:11.070 --> 04:16.680
I'm also going to go ahead and remove all of the comments here and I'm going to make CBC required as

04:16.680 --> 04:17.160
well.

04:17.160 --> 04:23.070
So we'll go ahead and validate the CBC here and make sure that this is a string as well as we'll say,

04:23.070 --> 04:25.290
this will not be an empty string.

04:25.290 --> 04:31.230
And now for the expiration month, we'll say that this will be a number here and same for the expiration

04:31.260 --> 04:31.560
year.

04:31.560 --> 04:34.740
We will also say that this will be of type is number.

04:34.740 --> 04:39.870
And then for the actual credit card number here, there's actually a custom validator we can use here

04:39.870 --> 04:42.930
to actually check to see if this is a valid credit card number.

04:42.930 --> 04:47.400
So we'll go ahead and import this from class validator as well.

04:47.400 --> 04:53.760
So now back in our Create Reservations, DTO, we can specify the type of the card here will be of type

04:53.760 --> 04:56.850
card DTO from our new DTO file.

04:56.850 --> 05:01.470
And importantly, we're going to go ahead and apply some validation to this itself.

05:01.470 --> 05:05.520
Will firstly say that this is actually be defined here.

05:05.520 --> 05:12.270
So we'll have the is defined from class validator as well as is not empty object to make sure this is

05:12.270 --> 05:18.210
an actual object and then we're going to use the validate nested property here which will ensure that

05:18.210 --> 05:23.820
it will actually get validated on the properties that live inside of this DTO or the ones that we just

05:23.820 --> 05:24.330
defined.

05:24.330 --> 05:28.890
And now we have everything we need to actually supply this card in a DTO.

05:28.920 --> 05:33.840
Lastly, we're going to add our amount field here, which is of type number, and this will be the actual

05:33.840 --> 05:36.120
cost of this reservation itself.

05:36.120 --> 05:39.630
So we'll go ahead and make sure that this is a number here.

05:39.630 --> 05:42.870
So this is great and should work perfectly fine in our case.

05:42.870 --> 05:48.150
However, if you notice right now, we actually have some duplication because in this crate reservation,

05:48.150 --> 05:55.800
DTO, we're essentially defining this set of properties here that we're passing to the payments controller.

05:56.420 --> 06:00.720
And we've already defined this DTO here as a create charge DTO.

06:01.270 --> 06:06.550
Now we want to avoid this duplication, but we also want to eventually implement validation on this

06:06.550 --> 06:07.510
payments controller.

06:07.510 --> 06:12.670
In this route here, we want to actually validate the incoming payload on our create charge route.

06:12.670 --> 06:18.100
So in order to accomplish both of these tasks, we're just going to move the newly created card DTO

06:18.130 --> 06:21.880
into our Libs common folder, into our DTO directory.

06:21.910 --> 06:30.310
We'll move this card DTO in here and then I'll also go ahead and move our create charge DTO into the

06:30.310 --> 06:31.630
DTO folder.

06:31.630 --> 06:39.880
Now in the create charge, DTO instead of the card here referencing the stripe type, we will make this

06:39.880 --> 06:47.740
of type card DTO and then we'll go ahead and copy over the annotations from this card DTO and move it

06:47.740 --> 06:48.580
to the common one.

06:48.580 --> 06:53.980
Here we'll add the annotations to the create charge DTO here or we have the card.

06:54.010 --> 06:58.660
We'll just need to go ahead and make sure that we import these from class validator.

06:58.660 --> 07:01.940
And then the amount here will also, of course be a number.

07:01.940 --> 07:07.940
So now in our index.ts, I'll go ahead and make sure we export everything from the create charge.

07:07.940 --> 07:13.910
DTO now back in the Create Reservation, DTO, let's go ahead and get rid of these two properties and

07:13.910 --> 07:22.040
we'll add a new object here called the Charge property, which we know will be of type create charge

07:22.070 --> 07:29.450
DTO and we will add the same annotations we did earlier where we say this is defined, it's not an empty

07:29.480 --> 07:33.890
object and we want to go ahead and validate nested here.

07:33.890 --> 07:37.760
So now we have a cleaner DTO here for our reservation.

07:37.760 --> 07:44.600
I'll go ahead and clean up our imports here and make sure we import this from app slash Common and then

07:44.600 --> 07:47.510
I'll go back into our payments controller here.

07:47.510 --> 07:52.490
And I'll also just change the import here to be from app slash common as well.

07:52.490 --> 07:58.310
So now our details are exactly the same in both our reservation and payment service and we have consistency

07:58.310 --> 08:00.620
with our types in one place, which is great.

08:00.620 --> 08:07.250
So let's go ahead and test out our updated DTO, I'm going to go ahead and make sure I log in first

08:07.280 --> 08:10.250
on our auth service to get our JWT cookie.

08:10.280 --> 08:15.980
So let's go ahead and post to 3001 all slash login with a valid user here.

08:16.070 --> 08:21.560
I'll go ahead and send this off and I have a 201 and I can see my JWT cookie.

08:21.590 --> 08:28.850
So now we'll send a request to localhost 3000 slash reservations and if we immediately post this off

08:28.850 --> 08:30.260
we should get a bad request.

08:30.260 --> 08:36.650
You can see we are complaining and importantly, we can see we have validation about the charge itself.

08:36.650 --> 08:39.170
So let's go ahead and fill out our DTO here.

08:39.200 --> 08:39.530
Okay.

08:39.530 --> 08:43.910
So I've gone ahead and partially filled out the DTO here, left the card blank.

08:43.940 --> 08:48.920
If we send the request off here, we can see we're getting a weird error message about an unknown value

08:48.920 --> 08:51.020
being passed to the validate function.

08:51.020 --> 08:56.770
And the reason for this is because we're not actually converting this property here into the create

08:56.780 --> 09:00.710
charge DTO object itself or instantiating a new class.

09:00.710 --> 09:04.720
Instead, we're just passing a plain JavaScript object, which is not what we want to do.

09:04.750 --> 09:09.340
We want to do what we did up here with our date and convert it to an actual object.

09:09.340 --> 09:15.310
So we'll use the class transformer type decorator here and actually return the type we want to turn

09:15.310 --> 09:16.240
this into.

09:16.270 --> 09:17.650
In this case the create charge.

09:17.650 --> 09:23.050
DTO if we follow the create charge DTO further on, we can do the same thing for the card.

09:23.050 --> 09:23.650
Here.

09:23.650 --> 09:27.010
We want to use class transformer to actually turn this into a card.

09:27.010 --> 09:33.370
DTO So now if I go off and send this request again, you can see we get the expected validation messages

09:33.370 --> 09:34.900
for our card object.

09:34.900 --> 09:37.840
So let's go ahead and finish completing this card.

09:37.870 --> 09:44.500
Okay, so I've gone ahead and completed the full DTO here now with some sample credit card information

09:44.500 --> 09:47.200
as well as changing my amount out to a real number.

09:47.200 --> 09:52.750
And now if we send the request off, we can see our validation is passing and we are able to pass this

09:52.750 --> 09:55.570
charge object along with our DTO.

09:55.570 --> 10:02.080
So now we can actually pass this to our payment service directly inside of our reservation service.
