Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): Implement CustomerGroup entity
- Loading branch information
1 parent
5ea09ef
commit ff495fc
Showing
12 changed files
with
321 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; | ||
import { | ||
AddCustomersToGroupMutationArgs, | ||
CreateCustomerGroupMutationArgs, | ||
CustomerGroupQueryArgs, | ||
Permission, | ||
RemoveCustomersFromGroupMutationArgs, | ||
UpdateCustomerGroupMutationArgs, | ||
} from 'shared/generated-types'; | ||
|
||
import { CustomerGroup } from '../../entity/customer-group/customer-group.entity'; | ||
import { CustomerGroupService } from '../../service/providers/customer-group.service'; | ||
import { Allow } from '../common/auth-guard'; | ||
import { Decode } from '../common/id-interceptor'; | ||
import { RequestContext } from '../common/request-context'; | ||
import { Ctx } from '../common/request-context.decorator'; | ||
|
||
@Resolver('CustomerGroup') | ||
export class CustomerGroupResolver { | ||
constructor(private customerGroupService: CustomerGroupService) {} | ||
|
||
@Query() | ||
@Allow(Permission.ReadCustomer) | ||
customerGroups(@Ctx() ctx: RequestContext): Promise<CustomerGroup[]> { | ||
return this.customerGroupService.findAll(); | ||
} | ||
|
||
@Query() | ||
@Allow(Permission.ReadCustomer) | ||
async customerGroup( | ||
@Ctx() ctx: RequestContext, | ||
@Args() args: CustomerGroupQueryArgs, | ||
): Promise<CustomerGroup | undefined> { | ||
return this.customerGroupService.findOne(args.id); | ||
} | ||
|
||
@Mutation() | ||
@Allow(Permission.CreateCustomer) | ||
@Decode('customerIds') | ||
async createCustomerGroup(@Args() args: CreateCustomerGroupMutationArgs): Promise<CustomerGroup> { | ||
return this.customerGroupService.create(args.input); | ||
} | ||
|
||
@Mutation() | ||
@Allow(Permission.UpdateCustomer) | ||
async updateCustomerGroup(@Args() args: UpdateCustomerGroupMutationArgs): Promise<CustomerGroup> { | ||
return this.customerGroupService.update(args.input); | ||
} | ||
|
||
@Mutation() | ||
@Allow(Permission.UpdateCustomer) | ||
@Decode('customerGroupId', 'customerIds') | ||
async addCustomersToGroup(@Args() args: AddCustomersToGroupMutationArgs): Promise<CustomerGroup> { | ||
return this.customerGroupService.addCustomersToGroup(args); | ||
} | ||
|
||
@Mutation() | ||
@Allow(Permission.UpdateCustomer) | ||
@Decode('customerGroupId', 'customerIds') | ||
async removeCustomersFromGroup( | ||
@Args() args: RemoveCustomersFromGroupMutationArgs, | ||
): Promise<CustomerGroup> { | ||
return this.customerGroupService.removeCustomersFromGroup(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
type Query { | ||
customerGroups: [CustomerGroup!]! | ||
customerGroup(id: ID!): CustomerGroup | ||
} | ||
|
||
type Mutation { | ||
"Create a new CustomerGroup" | ||
createCustomerGroup(input: CreateCustomerGroupInput!): CustomerGroup! | ||
"Update an existing CustomerGroup" | ||
updateCustomerGroup(input: UpdateCustomerGroupInput!): CustomerGroup! | ||
"Add Customers to a CustomerGroup" | ||
addCustomersToGroup(customerGroupId: ID!, customerIds: [ID!]!): CustomerGroup! | ||
"Remove Customers from a CustomerGroup" | ||
removeCustomersFromGroup(customerGroupId: ID!, customerIds: [ID!]!): CustomerGroup! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DeepPartial } from 'shared/shared-types'; | ||
import { Column, Entity, ManyToMany } from 'typeorm'; | ||
|
||
import { VendureEntity } from '../base/base.entity'; | ||
import { Customer } from '../customer/customer.entity'; | ||
|
||
@Entity() | ||
export class CustomerGroup extends VendureEntity { | ||
constructor(input?: DeepPartial<CustomerGroup>) { | ||
super(input); | ||
} | ||
|
||
@Column() name: string; | ||
|
||
@ManyToMany(type => Customer) | ||
customers: Customer[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
type CustomerGroup implements Node { | ||
id: ID! | ||
createdAt: DateTime! | ||
updatedAt: DateTime! | ||
name: String! | ||
} | ||
|
||
input CreateCustomerGroupInput { | ||
name: String! | ||
customerIds: [ID!] | ||
} | ||
|
||
input UpdateCustomerGroupInput { | ||
id: ID! | ||
name: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectConnection } from '@nestjs/typeorm'; | ||
import { | ||
AddCustomersToGroupMutationArgs, | ||
CreateCustomerGroupInput, | ||
RemoveCustomersFromGroupMutationArgs, | ||
UpdateCustomerGroupInput, | ||
} from 'shared/generated-types'; | ||
import { ID } from 'shared/shared-types'; | ||
import { unique } from 'shared/unique'; | ||
import { Connection } from 'typeorm'; | ||
|
||
import { assertFound } from '../../common/utils'; | ||
import { CustomerGroup } from '../../entity/customer-group/customer-group.entity'; | ||
import { Customer } from '../../entity/customer/customer.entity'; | ||
import { I18nError } from '../../i18n/i18n-error'; | ||
import { patchEntity } from '../helpers/patch-entity'; | ||
|
||
@Injectable() | ||
export class CustomerGroupService { | ||
constructor(@InjectConnection() private connection: Connection) {} | ||
|
||
findAll(): Promise<CustomerGroup[]> { | ||
return this.connection.getRepository(CustomerGroup).find({}); | ||
} | ||
|
||
findOne(customerGroupId: ID): Promise<CustomerGroup | undefined> { | ||
return this.connection.getRepository(CustomerGroup).findOne(customerGroupId); | ||
} | ||
|
||
async create(input: CreateCustomerGroupInput): Promise<CustomerGroup> { | ||
const customerGroup = new CustomerGroup(input); | ||
if (input.customerIds) { | ||
customerGroup.customers = await this.getCustomersFromIds(input.customerIds); | ||
} | ||
const newCustomerGroup = await this.connection.getRepository(CustomerGroup).save(customerGroup); | ||
return assertFound(this.findOne(newCustomerGroup.id)); | ||
} | ||
|
||
async update(input: UpdateCustomerGroupInput): Promise<CustomerGroup> { | ||
const customerGroup = await this.getCustomerGroupOrThrow(input.id); | ||
const updatedCustomerGroup = patchEntity(customerGroup, input); | ||
await this.connection.getRepository(CustomerGroup).save(updatedCustomerGroup); | ||
return assertFound(this.findOne(customerGroup.id)); | ||
} | ||
|
||
async addCustomersToGroup(input: AddCustomersToGroupMutationArgs): Promise<CustomerGroup> { | ||
const countries = await this.getCustomersFromIds(input.customerIds); | ||
const customerGroup = await this.getCustomerGroupOrThrow(input.customerGroupId); | ||
const customers = unique(customerGroup.customers.concat(countries), 'id'); | ||
customerGroup.customers = customers; | ||
await this.connection.getRepository(CustomerGroup).save(customerGroup); | ||
return customerGroup; | ||
} | ||
|
||
async removeCustomersFromGroup(input: RemoveCustomersFromGroupMutationArgs): Promise<CustomerGroup> { | ||
const customerGroup = await this.getCustomerGroupOrThrow(input.customerGroupId); | ||
customerGroup.customers = customerGroup.customers.filter( | ||
customer => !input.customerIds.includes(customer.id as string), | ||
); | ||
await this.connection.getRepository(CustomerGroup).save(customerGroup); | ||
return customerGroup; | ||
} | ||
|
||
private async getCustomerGroupOrThrow(id: ID): Promise<CustomerGroup> { | ||
const customerGroup = await this.findOne(id); | ||
if (!customerGroup) { | ||
throw new I18nError(`error.entity-with-id-not-found`, { entityName: 'CustomerGroup', id }); | ||
} | ||
return customerGroup; | ||
} | ||
|
||
private getCustomersFromIds(ids: ID[]): Promise<Customer[]> { | ||
return this.connection.getRepository(Customer).findByIds(ids); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.