Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Customer names fix (#13)
Browse files Browse the repository at this point in the history
* Remake Customer Model and relevant services

* Improve Ticket service

* Fix problem with Customer name

* Fix owner
  • Loading branch information
lukynmatuska authored Sep 23, 2022
1 parent 654f606 commit 43ea6fe
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
15 changes: 13 additions & 2 deletions src/models/Customer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class CustomerModel {
_id: string;

@Property()
name: CustomerNameSchema;
names: CustomerNameSchema[];

@Property()
@Required()
Expand All @@ -19,6 +19,17 @@ export class CustomerModel {
}

export class CustomerUpdateModel {
name?: CustomerNameUpdateSchema;
names?: CustomerNameUpdateSchema[];
email?: string;
}

export class CustomerEasyTicketModel {

@Property()
name: CustomerNameSchema;

@Property()
@Required()
@Unique()
email: string;
}
4 changes: 2 additions & 2 deletions src/models/Ticket.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model, ObjectID, Ref } from "@tsed/mongoose";
import { Default, Enum, Format, Property, Required } from "@tsed/schema";
import { CustomerModel } from "./Customer.model";
import { CustomerEasyTicketModel, CustomerModel } from "./Customer.model";
import { TimeModel } from "./Time.model";
import { YearModel } from "./Year.model";

Expand Down Expand Up @@ -59,7 +59,7 @@ export class TicketUpdateModel {

export class TicketEasyModel {
@Property()
buyer: CustomerModel;
buyer: CustomerEasyTicketModel;

// Team from YearModel.times
@Property()
Expand Down
10 changes: 2 additions & 8 deletions src/services/Customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ export class CustomerService {
.exec();

if (obj) {
if (update.name) {
if (update.name.first) {
obj.name.first = update.name.first;
}

if (update.name.last) {
obj.name.last = update.name.last;
}
if (update.names) {
obj.names = update.names;
}

if (update.email) {
Expand Down
19 changes: 15 additions & 4 deletions src/services/Ticket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ export class TicketService {
let customer = await this.customerModel.findOne({ email: obj.buyer.email.trim() });
let owner;
if (customer) {
owner = customer;
const foundName = customer.names.some(name => {
return (name.first == obj.buyer.name.first) && (name.last == obj.buyer.name.last);
})
if (!foundName) {
customer.names.push(obj.buyer.name);
owner = await this.customerService.update(customer.id, customer);
} else {
owner = customer;
}
} else {
owner = await this.customerService.save(obj.buyer);
let buyer = new CustomerModel();
buyer.names = [obj.buyer.name];
buyer.email = obj.buyer.email;
owner = await this.customerService.save(buyer);
}

// Get active year
Expand All @@ -58,9 +69,9 @@ export class TicketService {
})
year = await year.save();
year = await year
.populate("times")
.populate(["times"])
//@ts-ignore
.execPopulate();
// .execPopulate();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/templates/new-reservation.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<p>zde je potvrzení Vaší rezervace:<br>
<br>
ID: <%= reservation._id %><br>
Křestní jméno: <%= reservation.owner.name.first %><br>
Příjmení: <%= reservation.owner.name.last %><br>
Křestní jméno: <%= reservation.owner.names[reservation.owner.names.length-1].first %><br>
Příjmení: <%= reservation.owner.names[reservation.owner.names.length-1].last %><br>
Email: <%= reservation.owner.email %><br>
Čas: <%= reservation.time.name %><br>
</p>
Expand Down Expand Up @@ -42,4 +42,4 @@
</p>
</body>

</html>
</html>
6 changes: 3 additions & 3 deletions src/templates/new-reservation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Dobrý den,
zde je potvrzení Vaší rezervace:

ID: <%= reservation._id %>
Křestní jméno: <%= reservation.owner.name.first %>
Příjmení: <%= reservation.owner.name.last %>
Křestní jméno: <%= reservation.owner.names[reservation.owner.names.length-1].first %>
Příjmení: <%= reservation.owner.names[reservation.owner.names.length-1].last %>
Email: <%= reservation.owner.email %>
Čas: <%= reservation.time.name %>

Expand All @@ -25,4 +25,4 @@ S přáním hezkého dne,
Pořadatelé Pohádkového lesa v Rudici

Email: pohles@rudickamladez.cz
www pohles.rudickamladez.cz
www pohles.rudickamladez.cz

0 comments on commit 43ea6fe

Please sign in to comment.