add partner and slug into package
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
} from "@/database/entities/package-itinerary-widget.entity";
|
||||
import { PackageItinerary } from "@/database/entities/package-itinerary.entity";
|
||||
import { Package } from "@/database/entities/package.entity";
|
||||
import { Partner } from "@/database/entities/partner.entity";
|
||||
import { TransportationClass } from "@/database/entities/transportation-class.entity";
|
||||
import { AdminPermission } from "@/database/enums/admin-permission.enum";
|
||||
import { PackageItineraryWidgetType } from "@/database/enums/package-itinerary-widget-type.enum";
|
||||
@@ -39,6 +40,7 @@ import type {
|
||||
} from "@/modules/package/package.types";
|
||||
import { wrap } from "@mikro-orm/core";
|
||||
import { Router, type Request, type Response } from "express";
|
||||
import slugify from "slugify";
|
||||
import { ulid } from "ulid";
|
||||
|
||||
export class PackageController extends Controller {
|
||||
@@ -61,13 +63,29 @@ export class PackageController extends Controller {
|
||||
Buffer.from(body.thumbnail, "base64"),
|
||||
);
|
||||
|
||||
const partner = await orm.em.findOne(Partner, { id: body.partner_id });
|
||||
if (!partner) {
|
||||
return res.status(404).json({
|
||||
data: null,
|
||||
errors: [
|
||||
{
|
||||
path: "partner_id",
|
||||
location: "body",
|
||||
message: "Partner not found.",
|
||||
},
|
||||
],
|
||||
} satisfies ErrorResponse);
|
||||
}
|
||||
|
||||
const package_ = orm.em.create(Package, {
|
||||
id: ulid(),
|
||||
slug: slugify(body.name),
|
||||
name: body.name,
|
||||
type: body.type,
|
||||
class: body.class,
|
||||
thumbnail: thumbnailFile.name,
|
||||
useFastTrain: body.use_fast_train,
|
||||
partner,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
@@ -156,10 +174,30 @@ export class PackageController extends Controller {
|
||||
}
|
||||
const body = parseBodyResult.data;
|
||||
|
||||
const partner = await orm.em.findOne(
|
||||
Partner,
|
||||
{ id: body.partner_id },
|
||||
{ populate: ["*"] },
|
||||
);
|
||||
if (!partner) {
|
||||
return res.status(404).json({
|
||||
data: null,
|
||||
errors: [
|
||||
{
|
||||
path: "partner_id",
|
||||
location: "body",
|
||||
message: "Partner not found.",
|
||||
},
|
||||
],
|
||||
} satisfies ErrorResponse);
|
||||
}
|
||||
|
||||
const package_ = await orm.em.findOne(
|
||||
Package,
|
||||
{ id: params.id },
|
||||
{ populate: ["*"] },
|
||||
{
|
||||
populate: ["*"],
|
||||
},
|
||||
);
|
||||
if (!package_) {
|
||||
return res.status(404).json({
|
||||
@@ -179,10 +217,12 @@ export class PackageController extends Controller {
|
||||
package_.thumbnail,
|
||||
);
|
||||
|
||||
package_.slug = slugify(body.name);
|
||||
package_.name = body.name;
|
||||
package_.type = body.type;
|
||||
package_.class = body.class;
|
||||
package_.useFastTrain = body.use_fast_train;
|
||||
package_.partner = partner;
|
||||
package_.updatedAt = new Date();
|
||||
|
||||
await orm.em.flush();
|
||||
|
||||
Reference in New Issue
Block a user