# Order

Let's begin by creating a resource for <mark style="color:blue;">**`Order`**</mark>.

```sh
nest g res domain/orders
```

We'll create the basic strucutre for <mark style="color:blue;">`Order`</mark>, beginning with the **enum** <mark style="color:blue;">`OrderStatus`</mark>. Let's do this by creating the file <mark style="color:purple;">orders</mark>/<mark style="color:purple;">enums</mark>/<mark style="color:purple;">order-status.enum</mark> with following content:

```typescript
export enum OrderStatus {
  AWAITING_PAYMENT = 'AWAITING_PAYMENT',
  AWAITING_SHIPMENT = 'AWAITING_SHIPMENT',
  SHIPPED = 'SHIPPED',
  IN_TRANSIT = 'IN_TRANSIT',
  COMPLETED = 'COMPLETED',
  CANCELED = 'CANCELED',
}
```

{% hint style="info" %}
We could also use the approach of an **immutable array** (tuple) and a **literal type**. This method will be explained in the **Extra module 5**.
{% endhint %}

Now, in the <mark style="color:purple;">order.entity</mark> file, define the attributes.

```typescript
id: number;
status: OrderStatus;
registryDates: RegistryDates;
```

Then, execute the necessary steps to finish assembling the new resource:

* In the **controller**, use the <mark style="color:blue;">`IdDto`</mark> wherever <mark style="color:blue;">`id`</mark> is being used
* Remove the **number conversion operator** (<mark style="color:blue;">`+`</mark>) below each occurrence
* Use <mark style="color:blue;">`PaginationDto`</mark> in the <mark style="color:blue;">`findAll()`</mark> method (and also in the **service**)
  * Default page size - 5
* Add to the **entity** the **ORM decorators** (<mark style="color:blue;">`@Entity()`</mark>, <mark style="color:blue;">`@Column()`</mark>, etc)
* Register the entity inside the <mark style="color:blue;">`TypeOrmModule`</mark> in the resource's own **module**

However, the enum is not automatically mapped to the database just with the <mark style="color:blue;">`@Column()`</mark> decorator. We need to explicitly define the enum in its options, like this:

```typescript
{ type: 'enum', enum: OrderStatus, default: OrderStatus.AWAITING_PAYMENT }
```

We'll now deal with the matter of **relationships**. One user may have **many** orders, and one order belongs to **one** user. We can see that this is a **one-to-many** relationship. So, let's begin on the user's side. In the <mark style="color:purple;">user.entity</mark> file, we'll define an attribute for his <mark style="color:blue;">`orders`</mark> and use the <mark style="color:blue;">`@OneToMany()`</mark> decorator to map this relationship.

```typescript
@OneToMany(() => Order, (order) => order.customer)
orders: Order[];
```

{% hint style="info" %}
The relationship decorator has two parameters:

* The entity on the other side
* The name through which this entity is referenced on the other side
  {% endhint %}

Now, let's deal with the order's side, defining the attribute for its <mark style="color:blue;">`customer`</mark> and using <mark style="color:blue;">`@ManyToOne()`</mark>.

```typescript
@ManyToOne(() => User, (customer) => customer.orders, { nullable: false })
customer: User;
```

{% hint style="info" %}
By default, a column representing a **foreign key** can be <mark style="color:blue;">`null`</mark>. As an order must always belong to a user, the <mark style="color:blue;">`nullable`</mark> option is disabled.
{% endhint %}

Let's now generate and run the migration <mark style="color:orange;">create-order</mark> through the process already known to us.

As a last step, let's make sure that, when a user is searched by id, his orders are also fetched. To do this, go back to the <mark style="color:blue;">`UsersService`</mark> and, in the <mark style="color:blue;">`findOne()`</mark> method, alter it like the following:

```typescript
const user = await this.usersRepository.findOne({
  where: { id },
  relations: {
    orders: true,
  },
});
```

{% hint style="info" %}
Notice that now the <mark style="color:blue;">`where`</mark> clause must be explicitly set.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Creating order entity, using enum and handling one to many relation
