@hed-hog/address 0.0.275 → 0.0.276
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hedhog/data/role.yaml +6 -6
- package/hedhog/data/route.yaml +19 -19
- package/hedhog/table/address.yaml +27 -27
- package/package.json +3 -3
- package/src/address.controller.ts +55 -55
- package/src/address.module.ts +19 -19
- package/src/address.service.ts +162 -162
- package/src/dto/create.dto.ts +55 -55
- package/src/dto/update.dto.ts +3 -3
- package/src/index.ts +4 -4
- package/src/language/en.json +22 -22
- package/src/language/pt.json +22 -22
package/hedhog/data/role.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
- slug: admin-address
|
|
2
|
-
name:
|
|
3
|
-
en: Address Administrator
|
|
4
|
-
pt: Administrador de Enderecos
|
|
5
|
-
description:
|
|
6
|
-
en: Administrator with full access to address management.
|
|
1
|
+
- slug: admin-address
|
|
2
|
+
name:
|
|
3
|
+
en: Address Administrator
|
|
4
|
+
pt: Administrador de Enderecos
|
|
5
|
+
description:
|
|
6
|
+
en: Administrator with full access to address management.
|
|
7
7
|
pt: Administrador com acesso total a gestao de enderecos.
|
package/hedhog/data/route.yaml
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
- url: /address
|
|
2
|
-
method: GET
|
|
3
|
-
relations: &a1
|
|
4
|
-
role:
|
|
5
|
-
- where:
|
|
6
|
-
slug: admin
|
|
7
|
-
- where:
|
|
8
|
-
slug: admin-address
|
|
9
|
-
- url: /address
|
|
10
|
-
method: POST
|
|
11
|
-
relations: *a1
|
|
12
|
-
- url: /address/:id
|
|
13
|
-
method: GET
|
|
14
|
-
relations: *a1
|
|
15
|
-
- url: /address/:id
|
|
16
|
-
method: PATCH
|
|
17
|
-
relations: *a1
|
|
18
|
-
- url: /address
|
|
19
|
-
method: DELETE
|
|
1
|
+
- url: /address
|
|
2
|
+
method: GET
|
|
3
|
+
relations: &a1
|
|
4
|
+
role:
|
|
5
|
+
- where:
|
|
6
|
+
slug: admin
|
|
7
|
+
- where:
|
|
8
|
+
slug: admin-address
|
|
9
|
+
- url: /address
|
|
10
|
+
method: POST
|
|
11
|
+
relations: *a1
|
|
12
|
+
- url: /address/:id
|
|
13
|
+
method: GET
|
|
14
|
+
relations: *a1
|
|
15
|
+
- url: /address/:id
|
|
16
|
+
method: PATCH
|
|
17
|
+
relations: *a1
|
|
18
|
+
- url: /address
|
|
19
|
+
method: DELETE
|
|
20
20
|
relations: *a1
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
table: address
|
|
2
|
-
columns:
|
|
3
|
-
- type: pk
|
|
4
|
-
- name: address_type
|
|
5
|
-
type: enum
|
|
6
|
-
values:
|
|
7
|
-
- residential
|
|
8
|
-
- commercial
|
|
9
|
-
- correspondence
|
|
10
|
-
- alternative
|
|
11
|
-
- work
|
|
12
|
-
- billing
|
|
13
|
-
- shipping
|
|
14
|
-
- name: country_code
|
|
15
|
-
- name: state
|
|
16
|
-
- name: city
|
|
17
|
-
- name: postal_code
|
|
18
|
-
- name: line1
|
|
19
|
-
- name: line2
|
|
20
|
-
- name: is_primary
|
|
21
|
-
type: boolean
|
|
22
|
-
default: false
|
|
23
|
-
- type: created_at
|
|
24
|
-
- type: updated_at
|
|
25
|
-
indexes:
|
|
26
|
-
- columns: [address_type, is_primary]
|
|
27
|
-
unique: true
|
|
1
|
+
table: address
|
|
2
|
+
columns:
|
|
3
|
+
- type: pk
|
|
4
|
+
- name: address_type
|
|
5
|
+
type: enum
|
|
6
|
+
values:
|
|
7
|
+
- residential
|
|
8
|
+
- commercial
|
|
9
|
+
- correspondence
|
|
10
|
+
- alternative
|
|
11
|
+
- work
|
|
12
|
+
- billing
|
|
13
|
+
- shipping
|
|
14
|
+
- name: country_code
|
|
15
|
+
- name: state
|
|
16
|
+
- name: city
|
|
17
|
+
- name: postal_code
|
|
18
|
+
- name: line1
|
|
19
|
+
- name: line2
|
|
20
|
+
- name: is_primary
|
|
21
|
+
type: boolean
|
|
22
|
+
default: false
|
|
23
|
+
- type: created_at
|
|
24
|
+
- type: updated_at
|
|
25
|
+
indexes:
|
|
26
|
+
- columns: [address_type, is_primary]
|
|
27
|
+
unique: true
|
|
28
28
|
where: is_primary = true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hed-hog/address",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.276",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
"@nestjs/core": "^11",
|
|
10
10
|
"@nestjs/jwt": "^11",
|
|
11
11
|
"@nestjs/mapped-types": "*",
|
|
12
|
-
"@hed-hog/core": "0.0.
|
|
12
|
+
"@hed-hog/core": "0.0.276",
|
|
13
13
|
"@hed-hog/api": "0.0.4",
|
|
14
|
+
"@hed-hog/api-pagination": "0.0.6",
|
|
14
15
|
"@hed-hog/api-locale": "0.0.13",
|
|
15
16
|
"@hed-hog/api-prisma": "0.0.5",
|
|
16
|
-
"@hed-hog/api-pagination": "0.0.6",
|
|
17
17
|
"@hed-hog/api-types": "0.0.1"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { DeleteDTO, Role } from '@hed-hog/api';
|
|
2
|
-
import { Locale } from '@hed-hog/api-locale';
|
|
3
|
-
import { Pagination } from '@hed-hog/api-pagination';
|
|
4
|
-
import {
|
|
5
|
-
Body,
|
|
6
|
-
Controller,
|
|
7
|
-
Delete,
|
|
8
|
-
Get,
|
|
9
|
-
Inject,
|
|
10
|
-
Param,
|
|
11
|
-
ParseIntPipe,
|
|
12
|
-
Patch,
|
|
13
|
-
Post,
|
|
14
|
-
forwardRef
|
|
15
|
-
} from '@nestjs/common';
|
|
16
|
-
import { AddressService } from './address.service';
|
|
17
|
-
import { CreateDTO } from './dto/create.dto';
|
|
18
|
-
import { UpdateDTO } from './dto/update.dto';
|
|
19
|
-
|
|
20
|
-
@Role()
|
|
21
|
-
@Controller('address')
|
|
22
|
-
export class AddressController {
|
|
23
|
-
constructor(
|
|
24
|
-
@Inject(forwardRef(() => AddressService))
|
|
25
|
-
private readonly addressService: AddressService
|
|
26
|
-
) {}
|
|
27
|
-
|
|
28
|
-
@Get()
|
|
29
|
-
async list(@Pagination() paginationParams) {
|
|
30
|
-
return this.addressService.list(paginationParams);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
@Get(':id')
|
|
34
|
-
async get(@Param('id', ParseIntPipe) id: number, @Locale() locale: string) {
|
|
35
|
-
return this.addressService.get(id, locale);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
@Post()
|
|
39
|
-
async create(@Body() data: CreateDTO, @Locale() locale: string) {
|
|
40
|
-
return this.addressService.create(data, locale);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
@Patch(':id')
|
|
44
|
-
async update(
|
|
45
|
-
@Param('id', ParseIntPipe) id: number,
|
|
46
|
-
@Body() data: UpdateDTO,
|
|
47
|
-
@Locale() locale: string
|
|
48
|
-
) {
|
|
49
|
-
return this.addressService.update(id, data, locale);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
@Delete()
|
|
53
|
-
async delete(@Body() data: DeleteDTO, @Locale() locale: string) {
|
|
54
|
-
return this.addressService.delete(data, locale);
|
|
55
|
-
}
|
|
1
|
+
import { DeleteDTO, Role } from '@hed-hog/api';
|
|
2
|
+
import { Locale } from '@hed-hog/api-locale';
|
|
3
|
+
import { Pagination } from '@hed-hog/api-pagination';
|
|
4
|
+
import {
|
|
5
|
+
Body,
|
|
6
|
+
Controller,
|
|
7
|
+
Delete,
|
|
8
|
+
Get,
|
|
9
|
+
Inject,
|
|
10
|
+
Param,
|
|
11
|
+
ParseIntPipe,
|
|
12
|
+
Patch,
|
|
13
|
+
Post,
|
|
14
|
+
forwardRef
|
|
15
|
+
} from '@nestjs/common';
|
|
16
|
+
import { AddressService } from './address.service';
|
|
17
|
+
import { CreateDTO } from './dto/create.dto';
|
|
18
|
+
import { UpdateDTO } from './dto/update.dto';
|
|
19
|
+
|
|
20
|
+
@Role()
|
|
21
|
+
@Controller('address')
|
|
22
|
+
export class AddressController {
|
|
23
|
+
constructor(
|
|
24
|
+
@Inject(forwardRef(() => AddressService))
|
|
25
|
+
private readonly addressService: AddressService
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
@Get()
|
|
29
|
+
async list(@Pagination() paginationParams) {
|
|
30
|
+
return this.addressService.list(paginationParams);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@Get(':id')
|
|
34
|
+
async get(@Param('id', ParseIntPipe) id: number, @Locale() locale: string) {
|
|
35
|
+
return this.addressService.get(id, locale);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Post()
|
|
39
|
+
async create(@Body() data: CreateDTO, @Locale() locale: string) {
|
|
40
|
+
return this.addressService.create(data, locale);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@Patch(':id')
|
|
44
|
+
async update(
|
|
45
|
+
@Param('id', ParseIntPipe) id: number,
|
|
46
|
+
@Body() data: UpdateDTO,
|
|
47
|
+
@Locale() locale: string
|
|
48
|
+
) {
|
|
49
|
+
return this.addressService.update(id, data, locale);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@Delete()
|
|
53
|
+
async delete(@Body() data: DeleteDTO, @Locale() locale: string) {
|
|
54
|
+
return this.addressService.delete(data, locale);
|
|
55
|
+
}
|
|
56
56
|
}
|
package/src/address.module.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { forwardRef, Module } from '@nestjs/common';
|
|
2
|
-
import { ConfigModule } from '@nestjs/config';
|
|
3
|
-
import { LocaleModule } from '@hed-hog/api-locale';
|
|
4
|
-
import { PaginationModule } from '@hed-hog/api-pagination';
|
|
5
|
-
import { PrismaModule } from '@hed-hog/api-prisma';
|
|
6
|
-
import { AddressController } from './address.controller';
|
|
7
|
-
import { AddressService } from './address.service';
|
|
8
|
-
|
|
9
|
-
@Module({
|
|
10
|
-
imports: [
|
|
11
|
-
ConfigModule.forRoot(),
|
|
12
|
-
forwardRef(() => PaginationModule),
|
|
13
|
-
forwardRef(() => PrismaModule),
|
|
14
|
-
forwardRef(() => LocaleModule)
|
|
15
|
-
],
|
|
16
|
-
controllers: [AddressController],
|
|
17
|
-
providers: [AddressService],
|
|
18
|
-
exports: [AddressService]
|
|
19
|
-
})
|
|
1
|
+
import { forwardRef, Module } from '@nestjs/common';
|
|
2
|
+
import { ConfigModule } from '@nestjs/config';
|
|
3
|
+
import { LocaleModule } from '@hed-hog/api-locale';
|
|
4
|
+
import { PaginationModule } from '@hed-hog/api-pagination';
|
|
5
|
+
import { PrismaModule } from '@hed-hog/api-prisma';
|
|
6
|
+
import { AddressController } from './address.controller';
|
|
7
|
+
import { AddressService } from './address.service';
|
|
8
|
+
|
|
9
|
+
@Module({
|
|
10
|
+
imports: [
|
|
11
|
+
ConfigModule.forRoot(),
|
|
12
|
+
forwardRef(() => PaginationModule),
|
|
13
|
+
forwardRef(() => PrismaModule),
|
|
14
|
+
forwardRef(() => LocaleModule)
|
|
15
|
+
],
|
|
16
|
+
controllers: [AddressController],
|
|
17
|
+
providers: [AddressService],
|
|
18
|
+
exports: [AddressService]
|
|
19
|
+
})
|
|
20
20
|
export class AddressModule {}
|
package/src/address.service.ts
CHANGED
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
import { DeleteDTO } from '@hed-hog/api';
|
|
2
|
-
import { getLocaleText } from '@hed-hog/api-locale';
|
|
3
|
-
import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
|
|
4
|
-
import { PrismaService } from '@hed-hog/api-prisma';
|
|
5
|
-
import {
|
|
6
|
-
BadRequestException,
|
|
7
|
-
Inject,
|
|
8
|
-
Injectable,
|
|
9
|
-
NotFoundException,
|
|
10
|
-
forwardRef
|
|
11
|
-
} from '@nestjs/common';
|
|
12
|
-
import { CreateDTO } from './dto/create.dto';
|
|
13
|
-
import { UpdateDTO } from './dto/update.dto';
|
|
14
|
-
|
|
15
|
-
@Injectable()
|
|
16
|
-
export class AddressService {
|
|
17
|
-
constructor(
|
|
18
|
-
@Inject(forwardRef(() => PrismaService))
|
|
19
|
-
private readonly prismaService: PrismaService,
|
|
20
|
-
@Inject(forwardRef(() => PaginationService))
|
|
21
|
-
private readonly paginationService: PaginationService
|
|
22
|
-
) {}
|
|
23
|
-
|
|
24
|
-
async list(paginationParams: PaginationDTO) {
|
|
25
|
-
const search = paginationParams.search?.trim();
|
|
26
|
-
const orConditions: any[] = [];
|
|
27
|
-
|
|
28
|
-
if (search) {
|
|
29
|
-
if (!Number.isNaN(Number(search))) {
|
|
30
|
-
orConditions.push({ id: Number(search) });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
orConditions.push(
|
|
34
|
-
{ address_type: { equals: search } },
|
|
35
|
-
{ country_code: { contains: search, mode: 'insensitive' } },
|
|
36
|
-
{ state: { contains: search, mode: 'insensitive' } },
|
|
37
|
-
{ city: { contains: search, mode: 'insensitive' } },
|
|
38
|
-
{ postal_code: { contains: search, mode: 'insensitive' } },
|
|
39
|
-
{ line1: { contains: search, mode: 'insensitive' } },
|
|
40
|
-
{ line2: { contains: search, mode: 'insensitive' } }
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return this.paginationService.paginate(this.prismaService.address as any, paginationParams, {
|
|
45
|
-
where: orConditions.length > 0 ? { OR: orConditions } : {},
|
|
46
|
-
orderBy: { id: 'desc' }
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async get(id: number, locale: string) {
|
|
51
|
-
const address = await this.prismaService.address.findUnique({ where: { id } });
|
|
52
|
-
|
|
53
|
-
if (!address) {
|
|
54
|
-
throw new NotFoundException(
|
|
55
|
-
getLocaleText('addressNotFound', locale, `Address with ID ${id} not found`)
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return address;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async create(data: CreateDTO, locale: string) {
|
|
63
|
-
return this.prismaService.$transaction(async (tx) => {
|
|
64
|
-
await this.clearPrimaryForType(tx, data.address_type, data.is_primary === true);
|
|
65
|
-
|
|
66
|
-
return (tx.address as any).create({
|
|
67
|
-
data: {
|
|
68
|
-
...data,
|
|
69
|
-
is_primary: data.is_primary ?? false
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}).catch(() => {
|
|
73
|
-
throw new BadRequestException(
|
|
74
|
-
getLocaleText('addressNotCreated', locale, 'Address could not be created')
|
|
75
|
-
);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async update(id: number, data: UpdateDTO, locale: string) {
|
|
80
|
-
const existingAddress = await this.prismaService.address.findUnique({ where: { id } });
|
|
81
|
-
|
|
82
|
-
if (!existingAddress) {
|
|
83
|
-
throw new NotFoundException(
|
|
84
|
-
getLocaleText('addressNotFound', locale, `Address with ID ${id} not found`)
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const currentAddressType = (existingAddress as any).address_type;
|
|
89
|
-
const nextAddressType = data.address_type ?? currentAddressType;
|
|
90
|
-
const nextIsPrimary = data.is_primary ?? existingAddress.is_primary;
|
|
91
|
-
|
|
92
|
-
return this.prismaService.$transaction(async (tx) => {
|
|
93
|
-
await this.clearPrimaryForType(tx, nextAddressType, nextIsPrimary, id);
|
|
94
|
-
|
|
95
|
-
return (tx.address as any).update({
|
|
96
|
-
where: { id },
|
|
97
|
-
data
|
|
98
|
-
});
|
|
99
|
-
}).catch(() => {
|
|
100
|
-
throw new BadRequestException(
|
|
101
|
-
getLocaleText('addressNotUpdated', locale, 'Address could not be updated')
|
|
102
|
-
);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async delete({ ids }: DeleteDTO, locale: string) {
|
|
107
|
-
if (ids == undefined || ids == null) {
|
|
108
|
-
throw new BadRequestException(
|
|
109
|
-
getLocaleText('deleteItemsRequired', locale, 'You must select at least one item to delete.')
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const existingAddresses = await this.prismaService.address.findMany({
|
|
114
|
-
where: {
|
|
115
|
-
id: {
|
|
116
|
-
in: ids
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
select: {
|
|
120
|
-
id: true
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
if (existingAddresses.length !== ids.length) {
|
|
125
|
-
const existingIds = existingAddresses.map((item) => item.id);
|
|
126
|
-
const missingIds = ids.filter((itemId) => !existingIds.includes(itemId));
|
|
127
|
-
|
|
128
|
-
throw new NotFoundException(
|
|
129
|
-
getLocaleText('addressesNotFound', locale, `Addresses with IDs ${missingIds.join(', ')} not found`)
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return this.prismaService.address.deleteMany({
|
|
134
|
-
where: {
|
|
135
|
-
id: {
|
|
136
|
-
in: ids
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private async clearPrimaryForType(
|
|
143
|
-
tx: any,
|
|
144
|
-
addressType: string,
|
|
145
|
-
shouldClear: boolean,
|
|
146
|
-
excludeId?: number
|
|
147
|
-
) {
|
|
148
|
-
if (!shouldClear) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
await tx.address.updateMany({
|
|
153
|
-
where: {
|
|
154
|
-
address_type: addressType,
|
|
155
|
-
is_primary: true,
|
|
156
|
-
...(excludeId ? { id: { not: excludeId } } : {})
|
|
157
|
-
},
|
|
158
|
-
data: {
|
|
159
|
-
is_primary: false
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
}
|
|
1
|
+
import { DeleteDTO } from '@hed-hog/api';
|
|
2
|
+
import { getLocaleText } from '@hed-hog/api-locale';
|
|
3
|
+
import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
|
|
4
|
+
import { PrismaService } from '@hed-hog/api-prisma';
|
|
5
|
+
import {
|
|
6
|
+
BadRequestException,
|
|
7
|
+
Inject,
|
|
8
|
+
Injectable,
|
|
9
|
+
NotFoundException,
|
|
10
|
+
forwardRef
|
|
11
|
+
} from '@nestjs/common';
|
|
12
|
+
import { CreateDTO } from './dto/create.dto';
|
|
13
|
+
import { UpdateDTO } from './dto/update.dto';
|
|
14
|
+
|
|
15
|
+
@Injectable()
|
|
16
|
+
export class AddressService {
|
|
17
|
+
constructor(
|
|
18
|
+
@Inject(forwardRef(() => PrismaService))
|
|
19
|
+
private readonly prismaService: PrismaService,
|
|
20
|
+
@Inject(forwardRef(() => PaginationService))
|
|
21
|
+
private readonly paginationService: PaginationService
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
async list(paginationParams: PaginationDTO) {
|
|
25
|
+
const search = paginationParams.search?.trim();
|
|
26
|
+
const orConditions: any[] = [];
|
|
27
|
+
|
|
28
|
+
if (search) {
|
|
29
|
+
if (!Number.isNaN(Number(search))) {
|
|
30
|
+
orConditions.push({ id: Number(search) });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
orConditions.push(
|
|
34
|
+
{ address_type: { equals: search } },
|
|
35
|
+
{ country_code: { contains: search, mode: 'insensitive' } },
|
|
36
|
+
{ state: { contains: search, mode: 'insensitive' } },
|
|
37
|
+
{ city: { contains: search, mode: 'insensitive' } },
|
|
38
|
+
{ postal_code: { contains: search, mode: 'insensitive' } },
|
|
39
|
+
{ line1: { contains: search, mode: 'insensitive' } },
|
|
40
|
+
{ line2: { contains: search, mode: 'insensitive' } }
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return this.paginationService.paginate(this.prismaService.address as any, paginationParams, {
|
|
45
|
+
where: orConditions.length > 0 ? { OR: orConditions } : {},
|
|
46
|
+
orderBy: { id: 'desc' }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async get(id: number, locale: string) {
|
|
51
|
+
const address = await this.prismaService.address.findUnique({ where: { id } });
|
|
52
|
+
|
|
53
|
+
if (!address) {
|
|
54
|
+
throw new NotFoundException(
|
|
55
|
+
getLocaleText('addressNotFound', locale, `Address with ID ${id} not found`)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return address;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async create(data: CreateDTO, locale: string) {
|
|
63
|
+
return this.prismaService.$transaction(async (tx) => {
|
|
64
|
+
await this.clearPrimaryForType(tx, data.address_type, data.is_primary === true);
|
|
65
|
+
|
|
66
|
+
return (tx.address as any).create({
|
|
67
|
+
data: {
|
|
68
|
+
...data,
|
|
69
|
+
is_primary: data.is_primary ?? false
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}).catch(() => {
|
|
73
|
+
throw new BadRequestException(
|
|
74
|
+
getLocaleText('addressNotCreated', locale, 'Address could not be created')
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async update(id: number, data: UpdateDTO, locale: string) {
|
|
80
|
+
const existingAddress = await this.prismaService.address.findUnique({ where: { id } });
|
|
81
|
+
|
|
82
|
+
if (!existingAddress) {
|
|
83
|
+
throw new NotFoundException(
|
|
84
|
+
getLocaleText('addressNotFound', locale, `Address with ID ${id} not found`)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const currentAddressType = (existingAddress as any).address_type;
|
|
89
|
+
const nextAddressType = data.address_type ?? currentAddressType;
|
|
90
|
+
const nextIsPrimary = data.is_primary ?? existingAddress.is_primary;
|
|
91
|
+
|
|
92
|
+
return this.prismaService.$transaction(async (tx) => {
|
|
93
|
+
await this.clearPrimaryForType(tx, nextAddressType, nextIsPrimary, id);
|
|
94
|
+
|
|
95
|
+
return (tx.address as any).update({
|
|
96
|
+
where: { id },
|
|
97
|
+
data
|
|
98
|
+
});
|
|
99
|
+
}).catch(() => {
|
|
100
|
+
throw new BadRequestException(
|
|
101
|
+
getLocaleText('addressNotUpdated', locale, 'Address could not be updated')
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async delete({ ids }: DeleteDTO, locale: string) {
|
|
107
|
+
if (ids == undefined || ids == null) {
|
|
108
|
+
throw new BadRequestException(
|
|
109
|
+
getLocaleText('deleteItemsRequired', locale, 'You must select at least one item to delete.')
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const existingAddresses = await this.prismaService.address.findMany({
|
|
114
|
+
where: {
|
|
115
|
+
id: {
|
|
116
|
+
in: ids
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
select: {
|
|
120
|
+
id: true
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (existingAddresses.length !== ids.length) {
|
|
125
|
+
const existingIds = existingAddresses.map((item) => item.id);
|
|
126
|
+
const missingIds = ids.filter((itemId) => !existingIds.includes(itemId));
|
|
127
|
+
|
|
128
|
+
throw new NotFoundException(
|
|
129
|
+
getLocaleText('addressesNotFound', locale, `Addresses with IDs ${missingIds.join(', ')} not found`)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return this.prismaService.address.deleteMany({
|
|
134
|
+
where: {
|
|
135
|
+
id: {
|
|
136
|
+
in: ids
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private async clearPrimaryForType(
|
|
143
|
+
tx: any,
|
|
144
|
+
addressType: string,
|
|
145
|
+
shouldClear: boolean,
|
|
146
|
+
excludeId?: number
|
|
147
|
+
) {
|
|
148
|
+
if (!shouldClear) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
await tx.address.updateMany({
|
|
153
|
+
where: {
|
|
154
|
+
address_type: addressType,
|
|
155
|
+
is_primary: true,
|
|
156
|
+
...(excludeId ? { id: { not: excludeId } } : {})
|
|
157
|
+
},
|
|
158
|
+
data: {
|
|
159
|
+
is_primary: false
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
163
|
}
|
package/src/dto/create.dto.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { getLocaleText } from '@hed-hog/api-locale';
|
|
2
|
-
import { IsBoolean, IsEnum, IsOptional, IsString } from 'class-validator';
|
|
3
|
-
|
|
4
|
-
export enum AddressTypeEnum {
|
|
5
|
-
RESIDENTIAL = 'residential',
|
|
6
|
-
COMMERCIAL = 'commercial',
|
|
7
|
-
CORRESPONDENCE = 'correspondence',
|
|
8
|
-
ALTERNATIVE = 'alternative',
|
|
9
|
-
WORK = 'work',
|
|
10
|
-
BILLING = 'billing',
|
|
11
|
-
SHIPPING = 'shipping'
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class CreateDTO {
|
|
15
|
-
@IsEnum(AddressTypeEnum, {
|
|
16
|
-
message: (args) => getLocaleText('validation.addressTypeMustBeEnum', args.value)
|
|
17
|
-
})
|
|
18
|
-
address_type: AddressTypeEnum;
|
|
19
|
-
|
|
20
|
-
@IsString({
|
|
21
|
-
message: (args) => getLocaleText('validation.countryCodeMustBeString', args.value)
|
|
22
|
-
})
|
|
23
|
-
country_code: string;
|
|
24
|
-
|
|
25
|
-
@IsString({
|
|
26
|
-
message: (args) => getLocaleText('validation.stateMustBeString', args.value)
|
|
27
|
-
})
|
|
28
|
-
state: string;
|
|
29
|
-
|
|
30
|
-
@IsString({
|
|
31
|
-
message: (args) => getLocaleText('validation.cityMustBeString', args.value)
|
|
32
|
-
})
|
|
33
|
-
city: string;
|
|
34
|
-
|
|
35
|
-
@IsString({
|
|
36
|
-
message: (args) => getLocaleText('validation.postalCodeMustBeString', args.value)
|
|
37
|
-
})
|
|
38
|
-
postal_code: string;
|
|
39
|
-
|
|
40
|
-
@IsString({
|
|
41
|
-
message: (args) => getLocaleText('validation.line1MustBeString', args.value)
|
|
42
|
-
})
|
|
43
|
-
line1: string;
|
|
44
|
-
|
|
45
|
-
@IsOptional()
|
|
46
|
-
@IsString({
|
|
47
|
-
message: (args) => getLocaleText('validation.line2MustBeString', args.value)
|
|
48
|
-
})
|
|
49
|
-
line2?: string;
|
|
50
|
-
|
|
51
|
-
@IsOptional()
|
|
52
|
-
@IsBoolean({
|
|
53
|
-
message: (args) => getLocaleText('validation.isPrimaryMustBeBoolean', args.value)
|
|
54
|
-
})
|
|
55
|
-
is_primary?: boolean = false;
|
|
1
|
+
import { getLocaleText } from '@hed-hog/api-locale';
|
|
2
|
+
import { IsBoolean, IsEnum, IsOptional, IsString } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
export enum AddressTypeEnum {
|
|
5
|
+
RESIDENTIAL = 'residential',
|
|
6
|
+
COMMERCIAL = 'commercial',
|
|
7
|
+
CORRESPONDENCE = 'correspondence',
|
|
8
|
+
ALTERNATIVE = 'alternative',
|
|
9
|
+
WORK = 'work',
|
|
10
|
+
BILLING = 'billing',
|
|
11
|
+
SHIPPING = 'shipping'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class CreateDTO {
|
|
15
|
+
@IsEnum(AddressTypeEnum, {
|
|
16
|
+
message: (args) => getLocaleText('validation.addressTypeMustBeEnum', args.value)
|
|
17
|
+
})
|
|
18
|
+
address_type: AddressTypeEnum;
|
|
19
|
+
|
|
20
|
+
@IsString({
|
|
21
|
+
message: (args) => getLocaleText('validation.countryCodeMustBeString', args.value)
|
|
22
|
+
})
|
|
23
|
+
country_code: string;
|
|
24
|
+
|
|
25
|
+
@IsString({
|
|
26
|
+
message: (args) => getLocaleText('validation.stateMustBeString', args.value)
|
|
27
|
+
})
|
|
28
|
+
state: string;
|
|
29
|
+
|
|
30
|
+
@IsString({
|
|
31
|
+
message: (args) => getLocaleText('validation.cityMustBeString', args.value)
|
|
32
|
+
})
|
|
33
|
+
city: string;
|
|
34
|
+
|
|
35
|
+
@IsString({
|
|
36
|
+
message: (args) => getLocaleText('validation.postalCodeMustBeString', args.value)
|
|
37
|
+
})
|
|
38
|
+
postal_code: string;
|
|
39
|
+
|
|
40
|
+
@IsString({
|
|
41
|
+
message: (args) => getLocaleText('validation.line1MustBeString', args.value)
|
|
42
|
+
})
|
|
43
|
+
line1: string;
|
|
44
|
+
|
|
45
|
+
@IsOptional()
|
|
46
|
+
@IsString({
|
|
47
|
+
message: (args) => getLocaleText('validation.line2MustBeString', args.value)
|
|
48
|
+
})
|
|
49
|
+
line2?: string;
|
|
50
|
+
|
|
51
|
+
@IsOptional()
|
|
52
|
+
@IsBoolean({
|
|
53
|
+
message: (args) => getLocaleText('validation.isPrimaryMustBeBoolean', args.value)
|
|
54
|
+
})
|
|
55
|
+
is_primary?: boolean = false;
|
|
56
56
|
}
|
package/src/dto/update.dto.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PartialType } from '@nestjs/mapped-types';
|
|
2
|
-
import { CreateDTO } from './create.dto';
|
|
3
|
-
|
|
1
|
+
import { PartialType } from '@nestjs/mapped-types';
|
|
2
|
+
import { CreateDTO } from './create.dto';
|
|
3
|
+
|
|
4
4
|
export class UpdateDTO extends PartialType(CreateDTO) {}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './address.module';
|
|
2
|
-
export * from './address.service';
|
|
3
|
-
export * from './address.controller';
|
|
4
|
-
export * from './dto/create.dto';
|
|
1
|
+
export * from './address.module';
|
|
2
|
+
export * from './address.service';
|
|
3
|
+
export * from './address.controller';
|
|
4
|
+
export * from './dto/create.dto';
|
|
5
5
|
export * from './dto/update.dto';
|
package/src/language/en.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"deleteItemsRequired": "You must select at least one item to delete.",
|
|
3
|
-
"addressNotFound": "Address not found.",
|
|
4
|
-
"addressesNotFound": "Addresses not found.",
|
|
5
|
-
"addressNotCreated": "Address could not be created.",
|
|
6
|
-
"addressNotUpdated": "Address could not be updated.",
|
|
7
|
-
"validation": {
|
|
8
|
-
"addressTypeMustBeEnum": "address_type must be a valid enum value",
|
|
9
|
-
"countryCodeMustBeString": "country_code must be a string",
|
|
10
|
-
"stateMustBeString": "state must be a string",
|
|
11
|
-
"cityMustBeString": "city must be a string",
|
|
12
|
-
"postalCodeMustBeString": "postal_code must be a string",
|
|
13
|
-
"line1MustBeString": "line1 must be a string",
|
|
14
|
-
"line2MustBeString": "line2 must be a string",
|
|
15
|
-
"isPrimaryMustBeBoolean": "is_primary must be a boolean"
|
|
16
|
-
},
|
|
17
|
-
"HomePage": {
|
|
18
|
-
"title": "Address",
|
|
19
|
-
"description": "Manage your address.",
|
|
20
|
-
"breadcrumbHome": "Home",
|
|
21
|
-
"breadcrumbCurrent": "Address"
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"deleteItemsRequired": "You must select at least one item to delete.",
|
|
3
|
+
"addressNotFound": "Address not found.",
|
|
4
|
+
"addressesNotFound": "Addresses not found.",
|
|
5
|
+
"addressNotCreated": "Address could not be created.",
|
|
6
|
+
"addressNotUpdated": "Address could not be updated.",
|
|
7
|
+
"validation": {
|
|
8
|
+
"addressTypeMustBeEnum": "address_type must be a valid enum value",
|
|
9
|
+
"countryCodeMustBeString": "country_code must be a string",
|
|
10
|
+
"stateMustBeString": "state must be a string",
|
|
11
|
+
"cityMustBeString": "city must be a string",
|
|
12
|
+
"postalCodeMustBeString": "postal_code must be a string",
|
|
13
|
+
"line1MustBeString": "line1 must be a string",
|
|
14
|
+
"line2MustBeString": "line2 must be a string",
|
|
15
|
+
"isPrimaryMustBeBoolean": "is_primary must be a boolean"
|
|
16
|
+
},
|
|
17
|
+
"HomePage": {
|
|
18
|
+
"title": "Address",
|
|
19
|
+
"description": "Manage your address.",
|
|
20
|
+
"breadcrumbHome": "Home",
|
|
21
|
+
"breadcrumbCurrent": "Address"
|
|
22
|
+
}
|
|
23
23
|
}
|
package/src/language/pt.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"deleteItemsRequired": "Voce deve selecionar pelo menos um item para excluir.",
|
|
3
|
-
"addressNotFound": "Endereco nao encontrado.",
|
|
4
|
-
"addressesNotFound": "Enderecos nao encontrados.",
|
|
5
|
-
"addressNotCreated": "Nao foi possivel criar o endereco.",
|
|
6
|
-
"addressNotUpdated": "Nao foi possivel atualizar o endereco.",
|
|
7
|
-
"validation": {
|
|
8
|
-
"addressTypeMustBeEnum": "address_type deve ser um valor de enum valido",
|
|
9
|
-
"countryCodeMustBeString": "country_code deve ser uma string",
|
|
10
|
-
"stateMustBeString": "state deve ser uma string",
|
|
11
|
-
"cityMustBeString": "city deve ser uma string",
|
|
12
|
-
"postalCodeMustBeString": "postal_code deve ser uma string",
|
|
13
|
-
"line1MustBeString": "line1 deve ser uma string",
|
|
14
|
-
"line2MustBeString": "line2 deve ser uma string",
|
|
15
|
-
"isPrimaryMustBeBoolean": "is_primary deve ser um boolean"
|
|
16
|
-
},
|
|
17
|
-
"HomePage": {
|
|
18
|
-
"title": "Address",
|
|
19
|
-
"description": "Gerencie seu address.",
|
|
20
|
-
"breadcrumbHome": "Inicio",
|
|
21
|
-
"breadcrumbCurrent": "Address"
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"deleteItemsRequired": "Voce deve selecionar pelo menos um item para excluir.",
|
|
3
|
+
"addressNotFound": "Endereco nao encontrado.",
|
|
4
|
+
"addressesNotFound": "Enderecos nao encontrados.",
|
|
5
|
+
"addressNotCreated": "Nao foi possivel criar o endereco.",
|
|
6
|
+
"addressNotUpdated": "Nao foi possivel atualizar o endereco.",
|
|
7
|
+
"validation": {
|
|
8
|
+
"addressTypeMustBeEnum": "address_type deve ser um valor de enum valido",
|
|
9
|
+
"countryCodeMustBeString": "country_code deve ser uma string",
|
|
10
|
+
"stateMustBeString": "state deve ser uma string",
|
|
11
|
+
"cityMustBeString": "city deve ser uma string",
|
|
12
|
+
"postalCodeMustBeString": "postal_code deve ser uma string",
|
|
13
|
+
"line1MustBeString": "line1 deve ser uma string",
|
|
14
|
+
"line2MustBeString": "line2 deve ser uma string",
|
|
15
|
+
"isPrimaryMustBeBoolean": "is_primary deve ser um boolean"
|
|
16
|
+
},
|
|
17
|
+
"HomePage": {
|
|
18
|
+
"title": "Address",
|
|
19
|
+
"description": "Gerencie seu address.",
|
|
20
|
+
"breadcrumbHome": "Inicio",
|
|
21
|
+
"breadcrumbCurrent": "Address"
|
|
22
|
+
}
|
|
23
23
|
}
|