|
@@ -6,7 +6,7 @@ import {
|
|
CustomFieldWithValue,
|
|
CustomFieldWithValue,
|
|
} from "#api/v_0.1.0/types/custom-fields-types.js";
|
|
} from "#api/v_0.1.0/types/custom-fields-types.js";
|
|
import { DbSchema } from "#db/db-schema.js";
|
|
import { DbSchema } from "#db/db-schema.js";
|
|
-import { selPool, updPool } from "#db/db.js";
|
|
|
|
|
|
+import { selPool } from "#db/db.js";
|
|
import { ApiError } from "#exceptions/api-error.js";
|
|
import { ApiError } from "#exceptions/api-error.js";
|
|
import { logger } from "#plugins/logger.js";
|
|
import { logger } from "#plugins/logger.js";
|
|
import { DatabaseTransactionConnection, sql } from "slonik";
|
|
import { DatabaseTransactionConnection, sql } from "slonik";
|
|
@@ -288,8 +288,15 @@ where
|
|
return actReg;
|
|
return actReg;
|
|
}
|
|
}
|
|
|
|
|
|
- async getActRegForPeMember(activityRegId: string) {
|
|
|
|
- const actReg = await selPool.maybeOne(sql.type(
|
|
|
|
|
|
+ async getActRegForPeMember({
|
|
|
|
+ activityRegId,
|
|
|
|
+ tr,
|
|
|
|
+ }: {
|
|
|
|
+ activityRegId: string;
|
|
|
|
+ tr?: DatabaseTransactionConnection;
|
|
|
|
+ }) {
|
|
|
|
+ const db = this.getConnection(tr);
|
|
|
|
+ const actReg = await db.maybeOne(sql.type(
|
|
z.object({
|
|
z.object({
|
|
activityRegId: DbSchema.act.activityRegs.activityRegId,
|
|
activityRegId: DbSchema.act.activityRegs.activityRegId,
|
|
activityRegNumber: DbSchema.act.activityRegs.number,
|
|
activityRegNumber: DbSchema.act.activityRegs.number,
|
|
@@ -355,8 +362,15 @@ where
|
|
return initialRegStatusId;
|
|
return initialRegStatusId;
|
|
}
|
|
}
|
|
|
|
|
|
- async getActivity(activityCode: string) {
|
|
|
|
- return await selPool.maybeOne(sql.type(
|
|
|
|
|
|
+ async getActivity({
|
|
|
|
+ activityCode,
|
|
|
|
+ tr,
|
|
|
|
+ }: {
|
|
|
|
+ activityCode: string;
|
|
|
|
+ tr?: DatabaseTransactionConnection;
|
|
|
|
+ }) {
|
|
|
|
+ const db = this.getConnection(tr);
|
|
|
|
+ return await db.maybeOne(sql.type(
|
|
z.object({
|
|
z.object({
|
|
activityId: DbSchema.act.activities.activityId,
|
|
activityId: DbSchema.act.activities.activityId,
|
|
code: DbSchema.act.activities.code,
|
|
code: DbSchema.act.activities.code,
|
|
@@ -401,13 +415,23 @@ where
|
|
return !!isExist;
|
|
return !!isExist;
|
|
}
|
|
}
|
|
|
|
|
|
- async updateActRegPaymentStatus(activityRegId: string) {
|
|
|
|
- const actReg = await this.getActRegForPeMember(activityRegId);
|
|
|
|
|
|
+ async updateActRegPaymentStatus({
|
|
|
|
+ tr,
|
|
|
|
+ activityRegId,
|
|
|
|
+ }: {
|
|
|
|
+ tr: DatabaseTransactionConnection;
|
|
|
|
+ activityRegId: string;
|
|
|
|
+ }) {
|
|
|
|
+ logger.info(`Обновление статуса оплаты для регистрации ${activityRegId}`);
|
|
|
|
+ const actReg = await this.getActRegForPeMember({ activityRegId, tr });
|
|
if (!actReg) {
|
|
if (!actReg) {
|
|
throw ApiError.BadRequest("actRegNotFound", "Не найдена регистрация");
|
|
throw ApiError.BadRequest("actRegNotFound", "Не найдена регистрация");
|
|
}
|
|
}
|
|
|
|
|
|
- const activity = await this.getActivity(actReg.activityCode);
|
|
|
|
|
|
+ const activity = await this.getActivity({
|
|
|
|
+ activityCode: actReg.activityCode,
|
|
|
|
+ tr,
|
|
|
|
+ });
|
|
if (!activity) {
|
|
if (!activity) {
|
|
throw ApiError.BadRequest("activityNotFound", "Не найдена активность");
|
|
throw ApiError.BadRequest("activityNotFound", "Не найдена активность");
|
|
}
|
|
}
|
|
@@ -416,17 +440,18 @@ where
|
|
if (activity.paymentConfig === "PER_REGISTRATION") {
|
|
if (activity.paymentConfig === "PER_REGISTRATION") {
|
|
const isPaid = await this.checkActivityRegPayment({
|
|
const isPaid = await this.checkActivityRegPayment({
|
|
activityRegId,
|
|
activityRegId,
|
|
|
|
+ tr,
|
|
});
|
|
});
|
|
// если надо поменять
|
|
// если надо поменять
|
|
if (actReg.isPaid !== isPaid) {
|
|
if (actReg.isPaid !== isPaid) {
|
|
if (isPaid) {
|
|
if (isPaid) {
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
update act.activity_regs
|
|
update act.activity_regs
|
|
set is_paid = true
|
|
set is_paid = true
|
|
where activity_reg_id = ${activityRegId}
|
|
where activity_reg_id = ${activityRegId}
|
|
`);
|
|
`);
|
|
|
|
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
insert into act.act_reg_status_history (
|
|
insert into act.act_reg_status_history (
|
|
activity_reg_id,
|
|
activity_reg_id,
|
|
act_reg_status_id,
|
|
act_reg_status_id,
|
|
@@ -460,19 +485,20 @@ where
|
|
const isAllPaid = await this.checkMembersPayment({
|
|
const isAllPaid = await this.checkMembersPayment({
|
|
activityRegId,
|
|
activityRegId,
|
|
peId: actReg.peId,
|
|
peId: actReg.peId,
|
|
|
|
+ tr,
|
|
});
|
|
});
|
|
|
|
|
|
// если надо поменять
|
|
// если надо поменять
|
|
if (isAllPaid !== actReg.isPaid) {
|
|
if (isAllPaid !== actReg.isPaid) {
|
|
if (!isAllPaid) {
|
|
if (!isAllPaid) {
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
update act.activity_regs
|
|
update act.activity_regs
|
|
set is_paid = false
|
|
set is_paid = false
|
|
where activity_reg_id = ${activityRegId}
|
|
where activity_reg_id = ${activityRegId}
|
|
`);
|
|
`);
|
|
|
|
|
|
// TODO: Возможно стоит добавить в act.activities поле payment_status_id
|
|
// TODO: Возможно стоит добавить в act.activities поле payment_status_id
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
insert into act.act_reg_status_history (
|
|
insert into act.act_reg_status_history (
|
|
activity_reg_id,
|
|
activity_reg_id,
|
|
act_reg_status_id,
|
|
act_reg_status_id,
|
|
@@ -485,12 +511,12 @@ where
|
|
)
|
|
)
|
|
`);
|
|
`);
|
|
} else {
|
|
} else {
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
update act.activity_regs
|
|
update act.activity_regs
|
|
set is_paid = true
|
|
set is_paid = true
|
|
where activity_reg_id = ${activityRegId}
|
|
where activity_reg_id = ${activityRegId}
|
|
`);
|
|
`);
|
|
- await updPool.query(sql.unsafe`
|
|
|
|
|
|
+ await tr.query(sql.unsafe`
|
|
insert into act.act_reg_status_history (
|
|
insert into act.act_reg_status_history (
|
|
activity_reg_id,
|
|
activity_reg_id,
|
|
act_reg_status_id,
|
|
act_reg_status_id,
|
|
@@ -507,14 +533,21 @@ where
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private getConnection(connection?: DatabaseTransactionConnection) {
|
|
|
|
+ return connection || selPool;
|
|
|
|
+ }
|
|
|
|
+
|
|
async checkMembersPayment({
|
|
async checkMembersPayment({
|
|
peId,
|
|
peId,
|
|
activityRegId,
|
|
activityRegId,
|
|
|
|
+ tr,
|
|
}: {
|
|
}: {
|
|
peId: string;
|
|
peId: string;
|
|
activityRegId: string;
|
|
activityRegId: string;
|
|
|
|
+ tr?: DatabaseTransactionConnection;
|
|
}) {
|
|
}) {
|
|
- const members = await selPool.any(sql.type(
|
|
|
|
|
|
+ const db = this.getConnection(tr);
|
|
|
|
+ const members = await db.any(sql.type(
|
|
z.object({
|
|
z.object({
|
|
peMemberId: DbSchema.act.peMembers.peMemberId,
|
|
peMemberId: DbSchema.act.peMembers.peMemberId,
|
|
userId: DbSchema.act.peMembers.userId,
|
|
userId: DbSchema.act.peMembers.userId,
|
|
@@ -532,7 +565,7 @@ where
|
|
|
|
|
|
const memberIds = members.map((member) => member.peMemberId);
|
|
const memberIds = members.map((member) => member.peMemberId);
|
|
|
|
|
|
- const paidMemberRows = await selPool.any(sql.unsafe`
|
|
|
|
|
|
+ const paidMemberRows = await db.any(sql.unsafe`
|
|
select distinct
|
|
select distinct
|
|
oi.pe_member_id -- Выбираем ID тех, кто заплатил
|
|
oi.pe_member_id -- Выбираем ID тех, кто заплатил
|
|
from
|
|
from
|
|
@@ -546,8 +579,15 @@ where
|
|
return memberIds.length === paidMemberRows.length;
|
|
return memberIds.length === paidMemberRows.length;
|
|
}
|
|
}
|
|
|
|
|
|
- async checkActivityRegPayment({ activityRegId }: { activityRegId: string }) {
|
|
|
|
- const isPaid = await selPool.exists(sql.unsafe`
|
|
|
|
|
|
+ async checkActivityRegPayment({
|
|
|
|
+ activityRegId,
|
|
|
|
+ tr,
|
|
|
|
+ }: {
|
|
|
|
+ activityRegId: string;
|
|
|
|
+ tr?: DatabaseTransactionConnection;
|
|
|
|
+ }) {
|
|
|
|
+ const db = this.getConnection(tr);
|
|
|
|
+ const isPaid = await db.exists(sql.unsafe`
|
|
select 1
|
|
select 1
|
|
from shop.order_items oi
|
|
from shop.order_items oi
|
|
where oi.activity_reg_id = ${activityRegId}
|
|
where oi.activity_reg_id = ${activityRegId}
|
|
@@ -801,7 +841,8 @@ where
|
|
'mask', cfd.mask,
|
|
'mask', cfd.mask,
|
|
'options', cfd.options,
|
|
'options', cfd.options,
|
|
'isChangeResetStatus', f_1.is_change_reset_status,
|
|
'isChangeResetStatus', f_1.is_change_reset_status,
|
|
- 'validators', cfwv.validators
|
|
|
|
|
|
+ 'validators', cfwv.validators,
|
|
|
|
+ 'orderNumber', f_1.order_number
|
|
)) as fields
|
|
)) as fields
|
|
from
|
|
from
|
|
act.activity_reg_form_fields f_1
|
|
act.activity_reg_form_fields f_1
|
|
@@ -862,10 +903,14 @@ where
|
|
shop.order_items oi
|
|
shop.order_items oi
|
|
where
|
|
where
|
|
oi.activity_reg_id = ${actReg.activityRegId} and
|
|
oi.activity_reg_id = ${actReg.activityRegId} and
|
|
- oi.pe_member_id = ${peMember.peMemberId}
|
|
|
|
|
|
+ oi.pe_member_id = ${peMember.peMemberId} and
|
|
|
|
+ oi.status = 'PAID'
|
|
`);
|
|
`);
|
|
|
|
|
|
if (!orderItem) {
|
|
if (!orderItem) {
|
|
|
|
+ logger.error(
|
|
|
|
+ `Order item for activity reg ${actReg.activityRegId} and pe member ${peMember.peMemberId} not found`,
|
|
|
|
+ );
|
|
throw new Error("Order item not found");
|
|
throw new Error("Order item not found");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -916,9 +961,6 @@ where
|
|
z.object({
|
|
z.object({
|
|
orderItemId: DbSchema.shop.orderItems.orderItemId,
|
|
orderItemId: DbSchema.shop.orderItems.orderItemId,
|
|
orderId: DbSchema.shop.orderItems.orderId,
|
|
orderId: DbSchema.shop.orderItems.orderId,
|
|
- paymentId: DbSchema.shop.payments.paymentId,
|
|
|
|
- paymentExternalTransactionId:
|
|
|
|
- DbSchema.shop.payments.externalTransactionId,
|
|
|
|
}),
|
|
}),
|
|
)`
|
|
)`
|
|
select
|
|
select
|