# Recover

Let's return to the <mark style="color:blue;">`UsersController`</mark> to create a route for recovering a <mark style="color:blue;">`user`</mark> that was soft-deleted.

```typescript
@Patch(':id/recover')
recover(@Param() { id }: IdDto) {
  return this.usersService.recover(id);
}
```

In the <mark style="color:blue;">`UsersService`</mark>, we shall then create the <mark style="color:blue;">`recover()`</mark> method too. Here, we'll first find a <mark style="color:blue;">`user`</mark> in a very similar fashion to the <mark style="color:blue;">`findOne()`</mark> method, with the sole difference of using the <mark style="color:blue;">`withDeleted`</mark> option to actually find the soft-deleted record. After that, check if the user exists and if it's actually soft-deleted. Finally, recover it.

```typescript
async recover(id: number) {
  const user = await this.usersRepository.findOne({
    where: { id },
    relations: {
      orders: {
        items: true,
        payment: true,
      },
    },
    withDeleted: true,
  });

  if (!user) {
    throw new NotFoundException('User not found');
  }
  if (!user.registryDates.deletedAt) {
    throw new ConflictException('User not deleted');
  }

  return this.usersRepository.recover(user);
}
```

As a minor change, we could go back to the <mark style="color:blue;">`User`</mark> entity and have a <mark style="color:blue;">get</mark> field to tell if the user has been soft-deleted, by using a **double negation operator**, like so:

```typescript
get isDeleted() {
  return !!this.registryDates.deletedAt;
}
```

And then, use it when checking if the user has been soft-deleted. With this, we now have soft delete functionality in our system.

<mark style="color:green;">**Commit**</mark> - Applying soft delete
