forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomers.controller.ts
More file actions
124 lines (116 loc) · 3.23 KB
/
Copy pathcustomers.controller.ts
File metadata and controls
124 lines (116 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {
Controller,
Get,
Post,
Patch,
Delete,
Body,
Param,
Query,
} from "@nestjs/common";
import { Throttle } from "@nestjs/throttler";
import { CustomersService } from "./customers.service";
import { CreateCustomerDto } from "./dto/create-customer.dto";
import { UpdateCustomerDto } from "./dto/update-customer.dto";
import { Auth, CurrentUser } from "../auth/guard/auth.guard";
import { User } from "../users/user.entity";
import { PrismaService } from "../prisma/prisma.service";
/**
* Customers controller
* Manages client profiles for repeat invoicing.
*/
@Controller("customers")
export class CustomersController {
constructor(
private readonly customersService: CustomersService,
private readonly prisma: PrismaService,
) {}
/**
* Get all customers (with optional search filter)
*/
@Auth()
@Get()
async findAll(
@CurrentUser() user: User,
@Query("search") search?: string,
@Query("limit") limit?: string,
) {
const l = limit ? parseInt(limit, 10) : 50;
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.findAll(user.merchantId, search, l),
);
}
/**
* Search/autocomplete customers for typeahead UI
*/
@Auth()
@Get("search")
async search(
@CurrentUser() user: User,
@Query("q") q?: string,
@Query("limit") limit?: string,
) {
const l = limit ? parseInt(limit, 10) : 10;
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.search(user.merchantId, q ?? "", l),
);
}
/**
* Get a customer detail summary with business metrics.
*
* Returns invoice count, paid volume, outstanding balance, overdue balance,
* and recent invoice activity — all scoped to the merchant.
*/
@Auth()
@Get(":id/summary")
async getSummary(@CurrentUser() user: User, @Param("id") id: string) {
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.getCustomerSummary(user.merchantId, id),
);
}
/**
* Get a single customer by ID
*/
@Auth()
@Get(":id")
async findOne(@CurrentUser() user: User, @Param("id") id: string) {
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.findOne(user.merchantId, id),
);
}
/**
* Create a new customer profile
*/
@Post()
@Auth()
@Throttle({ default: { limit: 30, ttl: 3600 } })
async create(@CurrentUser() user: User, @Body() dto: CreateCustomerDto) {
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.create(user.merchantId, dto),
);
}
/**
* Update an existing customer profile
*/
@Patch(":id")
@Auth()
async update(
@CurrentUser() user: User,
@Param("id") id: string,
@Body() dto: UpdateCustomerDto,
) {
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.update(user.merchantId, id, dto),
);
}
/**
* Delete a customer profile
*/
@Delete(":id")
@Auth()
async remove(@CurrentUser() user: User, @Param("id") id: string) {
return await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.customersService.remove(user.merchantId, id),
);
}
}