163 lines
5.9 KiB
JavaScript
163 lines
5.9 KiB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
import { Body, Controller, Delete, Get, HttpCode, Injectable, Module, Param, Patch, Post, Query, Req, } from "@nestjs/common";
|
|
import { Throttle } from "@nestjs/throttler";
|
|
import { contactInputSchema, MESSAGE_STATUSES, messageUpdateSchema, } from "@imen/shared";
|
|
import { z } from "zod";
|
|
import { AdminAuth, CurrentUser, Roles } from "../common/auth.js";
|
|
import { AuditService } from "../common/common.module.js";
|
|
import { IdPipe, ZodPipe } from "../common/zod.pipe.js";
|
|
import { PrismaService } from "../prisma/prisma.service.js";
|
|
const listQuery = z.object({
|
|
status: z.enum(MESSAGE_STATUSES).optional(),
|
|
page: z.coerce.number().int().min(1).default(1),
|
|
pageSize: z.coerce.number().int().min(1).max(100).default(20),
|
|
});
|
|
export const toMessageDto = (m) => ({
|
|
id: m.id,
|
|
name: m.name,
|
|
org: m.org,
|
|
phone: m.phone,
|
|
message: m.message,
|
|
status: m.status,
|
|
createdAt: m.createdAt.toISOString(),
|
|
});
|
|
let ContactService = class ContactService {
|
|
prisma;
|
|
audit;
|
|
constructor(prisma, audit) {
|
|
this.prisma = prisma;
|
|
this.audit = audit;
|
|
}
|
|
async submit(input, ip, userAgent) {
|
|
if (input.website)
|
|
return;
|
|
await this.prisma.contactMessage.create({
|
|
data: {
|
|
name: input.name,
|
|
org: input.org,
|
|
phone: input.phone,
|
|
message: input.message,
|
|
ip: ip.slice(0, 64),
|
|
userAgent: userAgent.slice(0, 300),
|
|
},
|
|
});
|
|
}
|
|
async list(q) {
|
|
const where = q.status ? { status: q.status } : {};
|
|
const [items, total] = await this.prisma.$transaction([
|
|
this.prisma.contactMessage.findMany({
|
|
where,
|
|
orderBy: { id: "desc" },
|
|
skip: (q.page - 1) * q.pageSize,
|
|
take: q.pageSize,
|
|
}),
|
|
this.prisma.contactMessage.count({ where }),
|
|
]);
|
|
return { items: items.map(toMessageDto), total, page: q.page, pageSize: q.pageSize };
|
|
}
|
|
async setStatus(id, status) {
|
|
return toMessageDto(await this.prisma.contactMessage.update({ where: { id }, data: { status } }));
|
|
}
|
|
async remove(id, userId) {
|
|
const m = await this.prisma.contactMessage.delete({ where: { id } });
|
|
this.audit.log(userId, "delete", "message", id, m.name);
|
|
}
|
|
};
|
|
ContactService = __decorate([
|
|
Injectable(),
|
|
__metadata("design:paramtypes", [PrismaService,
|
|
AuditService])
|
|
], ContactService);
|
|
export { ContactService };
|
|
let PublicContactController = class PublicContactController {
|
|
contact;
|
|
constructor(contact) {
|
|
this.contact = contact;
|
|
}
|
|
async submit(body, req) {
|
|
await this.contact.submit(body, req.ip ?? "", req.headers["user-agent"] ?? "");
|
|
return { ok: true };
|
|
}
|
|
};
|
|
__decorate([
|
|
Post(),
|
|
HttpCode(201),
|
|
Throttle({ default: { limit: 5, ttl: 10 * 60_000 } }),
|
|
__param(0, Body(new ZodPipe(contactInputSchema))),
|
|
__param(1, Req()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object, Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], PublicContactController.prototype, "submit", null);
|
|
PublicContactController = __decorate([
|
|
Controller("public/contact"),
|
|
__metadata("design:paramtypes", [ContactService])
|
|
], PublicContactController);
|
|
export { PublicContactController };
|
|
let MessagesController = class MessagesController {
|
|
contact;
|
|
constructor(contact) {
|
|
this.contact = contact;
|
|
}
|
|
list(q) {
|
|
return this.contact.list(q);
|
|
}
|
|
update(id, body) {
|
|
return this.contact.setStatus(id, body.status);
|
|
}
|
|
async remove(id, user) {
|
|
await this.contact.remove(id, user.id);
|
|
}
|
|
};
|
|
__decorate([
|
|
Get(),
|
|
__param(0, Query(new ZodPipe(listQuery))),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], MessagesController.prototype, "list", null);
|
|
__decorate([
|
|
Patch(":id"),
|
|
__param(0, Param("id", IdPipe)),
|
|
__param(1, Body(new ZodPipe(messageUpdateSchema))),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], MessagesController.prototype, "update", null);
|
|
__decorate([
|
|
Roles("ADMIN"),
|
|
Delete(":id"),
|
|
HttpCode(204),
|
|
__param(0, Param("id", IdPipe)),
|
|
__param(1, CurrentUser()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], MessagesController.prototype, "remove", null);
|
|
MessagesController = __decorate([
|
|
AdminAuth(),
|
|
Controller("admin/messages"),
|
|
__metadata("design:paramtypes", [ContactService])
|
|
], MessagesController);
|
|
export { MessagesController };
|
|
let ContactModule = class ContactModule {
|
|
};
|
|
ContactModule = __decorate([
|
|
Module({
|
|
controllers: [PublicContactController, MessagesController],
|
|
providers: [ContactService],
|
|
})
|
|
], ContactModule);
|
|
export { ContactModule };
|
|
//# sourceMappingURL=contact.module.js.map
|