Compare commits
1 Commits
d29edf1f4e
...
export_exc
| Author | SHA1 | Date | |
|---|---|---|---|
| f1bdce317f |
@@ -15,12 +15,5 @@ DATABASE_URL="postgresql://postgres:local**@localhost:5432/caja_ahorro" #url con
|
|||||||
|
|
||||||
#Mail Configuration
|
#Mail Configuration
|
||||||
MAIL_HOST=gmail
|
MAIL_HOST=gmail
|
||||||
MAIL_USERNAME=
|
MAIL_USERNAME="123"
|
||||||
MAIL_PASSWORD=
|
MAIL_PASSWORD="123"
|
||||||
|
|
||||||
MINIO_ENDPOINT=
|
|
||||||
MINIO_PORT=
|
|
||||||
MINIO_ACCESS_KEY=
|
|
||||||
MINIO_SECRET_KEY=
|
|
||||||
MINIO_BUCKET=
|
|
||||||
MINIO_USE_SSL=
|
|
||||||
|
|||||||
@@ -42,17 +42,16 @@
|
|||||||
"@nestjs/platform-express": "11.0.0",
|
"@nestjs/platform-express": "11.0.0",
|
||||||
"dotenv": "16.5.0",
|
"dotenv": "16.5.0",
|
||||||
"drizzle-orm": "0.40.0",
|
"drizzle-orm": "0.40.0",
|
||||||
|
"exceljs": "^4.4.0",
|
||||||
"express": "5.1.0",
|
"express": "5.1.0",
|
||||||
"joi": "17.13.3",
|
"joi": "17.13.3",
|
||||||
"minio": "^8.0.6",
|
|
||||||
"moment": "2.30.1",
|
"moment": "2.30.1",
|
||||||
"path-to-regexp": "8.2.0",
|
"path-to-regexp": "8.2.0",
|
||||||
"pg": "8.13.3",
|
"pg": "8.13.3",
|
||||||
"pino-pretty": "13.0.0",
|
"pino-pretty": "13.0.0",
|
||||||
"reflect-metadata": "0.2.0",
|
"reflect-metadata": "0.2.0",
|
||||||
"rxjs": "7.8.1",
|
"rxjs": "7.8.1",
|
||||||
"sharp": "^0.34.5",
|
"sharp": "^0.34.5"
|
||||||
"xlsx-populate": "^1.21.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nestjs-modules/mailer": "^2.0.2",
|
"@nestjs-modules/mailer": "^2.0.2",
|
||||||
|
|||||||
@@ -10,17 +10,16 @@ import { ConfigModule } from '@nestjs/config';
|
|||||||
import { APP_GUARD } from '@nestjs/core';
|
import { APP_GUARD } from '@nestjs/core';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
import { ThrottlerGuard } from '@nestjs/throttler';
|
import { ThrottlerGuard } from '@nestjs/throttler';
|
||||||
import { MinioModule } from './common/minio/minio.module';
|
|
||||||
import { DrizzleModule } from './database/drizzle.module';
|
import { DrizzleModule } from './database/drizzle.module';
|
||||||
import { AuthModule } from './features/auth/auth.module';
|
import { AuthModule } from './features/auth/auth.module';
|
||||||
import { ConfigurationsModule } from './features/configurations/configurations.module';
|
import { ConfigurationsModule } from './features/configurations/configurations.module';
|
||||||
import { InventoryModule } from './features/inventory/inventory.module';
|
import { LocationModule } from './features/location/location.module'
|
||||||
import { LocationModule } from './features/location/location.module';
|
|
||||||
import { MailModule } from './features/mail/mail.module';
|
import { MailModule } from './features/mail/mail.module';
|
||||||
import { RolesModule } from './features/roles/roles.module';
|
import { RolesModule } from './features/roles/roles.module';
|
||||||
import { SurveysModule } from './features/surveys/surveys.module';
|
|
||||||
import { TrainingModule } from './features/training/training.module';
|
|
||||||
import { UserRolesModule } from './features/user-roles/user-roles.module';
|
import { UserRolesModule } from './features/user-roles/user-roles.module';
|
||||||
|
import { SurveysModule } from './features/surveys/surveys.module';
|
||||||
|
import { InventoryModule } from './features/inventory/inventory.module';
|
||||||
|
import { TrainingModule } from './features/training/training.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
@@ -52,7 +51,6 @@ import { UserRolesModule } from './features/user-roles/user-roles.module';
|
|||||||
NodeMailerModule,
|
NodeMailerModule,
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
ThrottleModule,
|
ThrottleModule,
|
||||||
MinioModule,
|
|
||||||
UsersModule,
|
UsersModule,
|
||||||
AuthModule,
|
AuthModule,
|
||||||
MailModule,
|
MailModule,
|
||||||
@@ -63,7 +61,7 @@ import { UserRolesModule } from './features/user-roles/user-roles.module';
|
|||||||
SurveysModule,
|
SurveysModule,
|
||||||
LocationModule,
|
LocationModule,
|
||||||
InventoryModule,
|
InventoryModule,
|
||||||
TrainingModule,
|
TrainingModule
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule { }
|
||||||
|
|||||||
@@ -14,12 +14,6 @@ interface EnvVars {
|
|||||||
MAIL_HOST: string;
|
MAIL_HOST: string;
|
||||||
MAIL_USERNAME: string;
|
MAIL_USERNAME: string;
|
||||||
MAIL_PASSWORD: string;
|
MAIL_PASSWORD: string;
|
||||||
MINIO_ENDPOINT: string;
|
|
||||||
MINIO_PORT: number;
|
|
||||||
MINIO_ACCESS_KEY: string;
|
|
||||||
MINIO_SECRET_KEY: string;
|
|
||||||
MINIO_BUCKET: string;
|
|
||||||
MINIO_USE_SSL: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const envsSchema = joi
|
const envsSchema = joi
|
||||||
@@ -36,12 +30,6 @@ const envsSchema = joi
|
|||||||
MAIL_HOST: joi.string(),
|
MAIL_HOST: joi.string(),
|
||||||
MAIL_USERNAME: joi.string(),
|
MAIL_USERNAME: joi.string(),
|
||||||
MAIL_PASSWORD: joi.string(),
|
MAIL_PASSWORD: joi.string(),
|
||||||
MINIO_ENDPOINT: joi.string().required(),
|
|
||||||
MINIO_PORT: joi.number().required(),
|
|
||||||
MINIO_ACCESS_KEY: joi.string().required(),
|
|
||||||
MINIO_SECRET_KEY: joi.string().required(),
|
|
||||||
MINIO_BUCKET: joi.string().required(),
|
|
||||||
MINIO_USE_SSL: joi.boolean().default(false),
|
|
||||||
})
|
})
|
||||||
.unknown(true);
|
.unknown(true);
|
||||||
|
|
||||||
@@ -66,10 +54,4 @@ export const envs = {
|
|||||||
mail_host: envVars.MAIL_HOST,
|
mail_host: envVars.MAIL_HOST,
|
||||||
mail_username: envVars.MAIL_USERNAME,
|
mail_username: envVars.MAIL_USERNAME,
|
||||||
mail_password: envVars.MAIL_PASSWORD,
|
mail_password: envVars.MAIL_PASSWORD,
|
||||||
minio_endpoint: envVars.MINIO_ENDPOINT,
|
|
||||||
minio_port: envVars.MINIO_PORT,
|
|
||||||
minio_access_key: envVars.MINIO_ACCESS_KEY,
|
|
||||||
minio_secret_key: envVars.MINIO_SECRET_KEY,
|
|
||||||
minio_bucket: envVars.MINIO_BUCKET,
|
|
||||||
minio_use_ssl: envVars.MINIO_USE_SSL,
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import { Global, Module } from '@nestjs/common';
|
|
||||||
import { MinioService } from './minio.service';
|
|
||||||
|
|
||||||
@Global()
|
|
||||||
@Module({
|
|
||||||
providers: [MinioService],
|
|
||||||
exports: [MinioService],
|
|
||||||
})
|
|
||||||
export class MinioModule {}
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
|
||||||
import * as Minio from 'minio';
|
|
||||||
import { envs } from '../config/envs';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class MinioService implements OnModuleInit {
|
|
||||||
private readonly minioClient: Minio.Client;
|
|
||||||
private readonly logger = new Logger(MinioService.name);
|
|
||||||
private readonly bucketName = envs.minio_bucket;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.minioClient = new Minio.Client({
|
|
||||||
endPoint: envs.minio_endpoint,
|
|
||||||
port: envs.minio_port,
|
|
||||||
useSSL: envs.minio_use_ssl,
|
|
||||||
accessKey: envs.minio_access_key,
|
|
||||||
secretKey: envs.minio_secret_key,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async onModuleInit() {
|
|
||||||
await this.ensureBucketExists();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async ensureBucketExists() {
|
|
||||||
// Ejecuta esto siempre al menos una vez para asegurar que sea público
|
|
||||||
const policy = {
|
|
||||||
Version: '2012-10-17',
|
|
||||||
Statement: [
|
|
||||||
{
|
|
||||||
Effect: 'Allow',
|
|
||||||
Principal: { AWS: ['*'] },
|
|
||||||
Action: ['s3:GetObject'],
|
|
||||||
Resource: [`arn:aws:s3:::${this.bucketName}/*`],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
// const bucketExists = await this.minioClient.bucketExists(this.bucketName);
|
|
||||||
// if (!bucketExists) {
|
|
||||||
// await this.minioClient.makeBucket(this.bucketName);
|
|
||||||
// }
|
|
||||||
|
|
||||||
await this.minioClient.setBucketPolicy(
|
|
||||||
this.bucketName,
|
|
||||||
JSON.stringify(policy),
|
|
||||||
);
|
|
||||||
this.logger.log(`Public policy ensured for bucket "${this.bucketName}"`);
|
|
||||||
} catch (error: any) {
|
|
||||||
this.logger.error(`Error checking/creating bucket: ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async upload(
|
|
||||||
file: Express.Multer.File,
|
|
||||||
folder: string = 'general',
|
|
||||||
): Promise<string> {
|
|
||||||
const fileName = `${Date.now()}-${Math.round(Math.random() * 1e9)}-${file.originalname.replace(/\s/g, '_')}`;
|
|
||||||
const objectName = `${folder}/${fileName}`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.minioClient.putObject(
|
|
||||||
this.bucketName,
|
|
||||||
objectName,
|
|
||||||
file.buffer,
|
|
||||||
file.size,
|
|
||||||
{
|
|
||||||
'Content-Type': file.mimetype,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Return the URL or the object path.
|
|
||||||
// Usually, we store the object path and generate a signed URL or use a proxy.
|
|
||||||
// The user asked for the URL to be stored in the database.
|
|
||||||
return objectName;
|
|
||||||
} catch (error: any) {
|
|
||||||
this.logger.error(`Error uploading file: ${error.message}`);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getFileUrl(objectName: string): Promise<string> {
|
|
||||||
try {
|
|
||||||
// If the bucket is public, we can just return the URL.
|
|
||||||
// If private, we need a signed URL.
|
|
||||||
// For simplicity and common use cases in these projects, I'll generate a signed URL with a long expiration
|
|
||||||
// or assume there is some way to access it.
|
|
||||||
// But let's use signed URL for 1 week (maximum is 7 days) if needed,
|
|
||||||
// or just return the object name if the backend handles the serving.
|
|
||||||
// The user wants the URL stored in the DB.
|
|
||||||
|
|
||||||
return await this.minioClient.presignedUrl(
|
|
||||||
'GET',
|
|
||||||
this.bucketName,
|
|
||||||
objectName,
|
|
||||||
604800,
|
|
||||||
);
|
|
||||||
} catch (error: any) {
|
|
||||||
this.logger.error(`Error getting file URL: ${error.message}`);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getPublicUrl(objectName: string): string {
|
|
||||||
const protocol = envs.minio_use_ssl ? 'https' : 'http';
|
|
||||||
return `${protocol}://${envs.minio_endpoint}:${envs.minio_port}/${this.bucketName}/${objectName}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(objectName: string): Promise<void> {
|
|
||||||
try {
|
|
||||||
// Ensure we don't have a leading slash which can cause issues with removeObject
|
|
||||||
const cleanedName = objectName.startsWith('/')
|
|
||||||
? objectName.slice(1)
|
|
||||||
: objectName;
|
|
||||||
|
|
||||||
await this.minioClient.removeObject(this.bucketName, cleanedName);
|
|
||||||
this.logger.log(
|
|
||||||
`Object "${cleanedName}" deleted successfully from bucket "${this.bucketName}".`,
|
|
||||||
);
|
|
||||||
} catch (error: any) {
|
|
||||||
this.logger.error(
|
|
||||||
`Error deleting file "${objectName}": ${error.message}`,
|
|
||||||
);
|
|
||||||
// We don't necessarily want to throw if the file is already gone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,17 +8,17 @@ import { ThrottlerModule } from '@nestjs/throttler';
|
|||||||
{
|
{
|
||||||
name: 'short',
|
name: 'short',
|
||||||
ttl: 1000, // 1 sec
|
ttl: 1000, // 1 sec
|
||||||
limit: 10,
|
limit: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'medium',
|
name: 'medium',
|
||||||
ttl: 10000, // 10 sec
|
ttl: 10000, // 10 sec
|
||||||
limit: 30,
|
limit: 4,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'long',
|
name: 'long',
|
||||||
ttl: 60000, // 1 min
|
ttl: 60000, // 1 min
|
||||||
limit: 100,
|
limit: 10,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
errorMessage: 'Too many requests, please try again later.',
|
errorMessage: 'Too many requests, please try again later.',
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE "auth"."sessions" ADD COLUMN "previous_session_token" varchar;--> statement-breakpoint
|
|
||||||
ALTER TABLE "auth"."sessions" ADD COLUMN "last_rotated_at" timestamp;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
ALTER TABLE "training_surveys" ADD COLUMN "created_by" integer;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "updated_by" integer;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD CONSTRAINT "training_surveys_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD CONSTRAINT "training_surveys_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "auth"."users"("id") ON DELETE cascade ON UPDATE no action;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_spokesperson_cedula" DROP DEFAULT;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_spokesperson_cedula" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_spokesperson_rif" DROP DEFAULT;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_spokesperson_rif" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_email" DROP DEFAULT;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "commune_email" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "communal_council_spokesperson_cedula" DROP DEFAULT;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "communal_council_spokesperson_cedula" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "communal_council_spokesperson_rif" DROP DEFAULT;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "communal_council_spokesperson_rif" DROP NOT NULL;
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
ALTER TABLE "training_surveys" ALTER COLUMN "osp_responsible_rif" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "civil_state" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "osp_responsible_email" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "family_burden" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "number_of_children" DROP NOT NULL;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE "training_surveys" ALTER COLUMN "osp_rif" DROP NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ALTER COLUMN "osp_name" DROP NOT NULL;
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
ALTER TABLE "training_surveys" ADD COLUMN "internal_distribution_zone" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "is_exporting" boolean DEFAULT false NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "external_country" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "external_city" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "external_description" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "external_quantity" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "external_unit" text;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "women_count" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "men_count" integer DEFAULT 0 NOT NULL;
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
DROP INDEX "training_surveys_index_00";--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "coor_full_name" text NOT NULL;--> statement-breakpoint
|
|
||||||
CREATE INDEX "training_surveys_index_00" ON "training_surveys" USING btree ("coor_full_name");--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" DROP COLUMN "firstname";--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" DROP COLUMN "lastname";
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
CREATE VIEW "public"."v_training_surveys" AS (select "id", "osp_name", "osp_rif", "osp_type", "current_status", "visit_date" from "training_surveys");
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
DROP VIEW "public"."v_training_surveys";--> statement-breakpoint
|
|
||||||
CREATE VIEW "public"."v_training_surveys" AS (select "id", "coor_full_name", "visit_date", "coor_phone", "state", "municipality", "parish", "osp_type", "eco_sector", "productive_sector", "central_productive_activity", "main_productive_activity", "productive_activity", "osp_rif", "osp_name", "company_constitution_year", "current_status", "infrastructure_mt2", "has_transport", "structure_type", "is_open_space", "paralysis_reason", "equipment_list", "production_list", "product_list", "osp_address", "osp_google_maps_link", "commune_name", "situr_code_commune", "commune_rif", "commune_spokesperson_name", "commune_spokesperson_cedula", "commune_spokesperson_rif", "commune_spokesperson_phone", "commune_email", "communal_council", "situr_code_communal_council", "communal_council_rif", "communal_council_spokesperson_name", "communal_council_spokesperson_cedula", "communal_council_spokesperson_rif", "communal_council_spokesperson_phone", "communal_council_email", "osp_responsible_fullname", "osp_responsible_cedula", "osp_responsible_rif", "civil_state", "osp_responsible_phone", "osp_responsible_email", "family_burden", "number_of_children", "general_observations", "internal_distribution_zone", "is_exporting", "external_country", "external_city", "external_description", "external_quantity", "external_unit", "women_count", "men_count", "photo1", "photo2", "photo3", "created_by", "updated_by", "created_at", "updated_at" from "training_surveys");
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
DROP VIEW "public"."v_training_surveys";--> statement-breakpoint
|
|
||||||
ALTER TABLE "training_surveys" ADD COLUMN "productive_activity_other" text DEFAULT '';
|
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -120,76 +120,6 @@
|
|||||||
"when": 1769653021994,
|
"when": 1769653021994,
|
||||||
"tag": "0016_silent_tag",
|
"tag": "0016_silent_tag",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 17,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1770774052351,
|
|
||||||
"tag": "0017_mute_mole_man",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 18,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1771855467870,
|
|
||||||
"tag": "0018_milky_prism",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 19,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1771858973096,
|
|
||||||
"tag": "0019_cuddly_cobalt_man",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 20,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1771897944334,
|
|
||||||
"tag": "0020_certain_bushwacker",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 21,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1771901546945,
|
|
||||||
"tag": "0021_warm_machine_man",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 22,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1772031518006,
|
|
||||||
"tag": "0022_nervous_dragon_lord",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 23,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1772032122473,
|
|
||||||
"tag": "0023_sticky_slayback",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 24,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1772642460042,
|
|
||||||
"tag": "0024_petite_sabra",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 25,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1772643066120,
|
|
||||||
"tag": "0025_funny_makkari",
|
|
||||||
"breakpoints": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 26,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1774379641691,
|
|
||||||
"tag": "0026_last_vampiro",
|
|
||||||
"breakpoints": true
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
import { sql } from 'drizzle-orm';
|
|
||||||
import * as t from 'drizzle-orm/pg-core';
|
import * as t from 'drizzle-orm/pg-core';
|
||||||
import { timestamps } from '../timestamps';
|
import { sql } from 'drizzle-orm';
|
||||||
import { municipalities, parishes, states } from './general';
|
|
||||||
import { authSchema } from './schemas';
|
import { authSchema } from './schemas';
|
||||||
|
import { timestamps } from '../timestamps';
|
||||||
|
import { states, municipalities, parishes } from './general';
|
||||||
|
|
||||||
|
|
||||||
// Tabla de Usuarios sistema
|
// Tabla de Usuarios sistema
|
||||||
export const users = authSchema.table(
|
export const users = authSchema.table(
|
||||||
@@ -14,15 +15,9 @@ export const users = authSchema.table(
|
|||||||
fullname: t.text('fullname').notNull(),
|
fullname: t.text('fullname').notNull(),
|
||||||
phone: t.text('phone'),
|
phone: t.text('phone'),
|
||||||
password: t.text('password').notNull(),
|
password: t.text('password').notNull(),
|
||||||
state: t
|
state: t.integer('state').references(() => states.id, { onDelete: 'set null' }),
|
||||||
.integer('state')
|
municipality: t.integer('municipality').references(() => municipalities.id, { onDelete: 'set null' }),
|
||||||
.references(() => states.id, { onDelete: 'set null' }),
|
parish: t.integer('parish').references(() => parishes.id, { onDelete: 'set null' }),
|
||||||
municipality: t
|
|
||||||
.integer('municipality')
|
|
||||||
.references(() => municipalities.id, { onDelete: 'set null' }),
|
|
||||||
parish: t
|
|
||||||
.integer('parish')
|
|
||||||
.references(() => parishes.id, { onDelete: 'set null' }),
|
|
||||||
isTwoFactorEnabled: t
|
isTwoFactorEnabled: t
|
||||||
.boolean('is_two_factor_enabled')
|
.boolean('is_two_factor_enabled')
|
||||||
.notNull()
|
.notNull()
|
||||||
@@ -37,6 +32,7 @@ export const users = authSchema.table(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// Tabla de Roles
|
// Tabla de Roles
|
||||||
export const roles = authSchema.table(
|
export const roles = authSchema.table(
|
||||||
'roles',
|
'roles',
|
||||||
@@ -50,6 +46,8 @@ export const roles = authSchema.table(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//tabla User_roles
|
//tabla User_roles
|
||||||
export const usersRole = authSchema.table(
|
export const usersRole = authSchema.table(
|
||||||
'user_role',
|
'user_role',
|
||||||
@@ -90,6 +88,7 @@ LEFT JOIN
|
|||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
auth.roles r ON ur.role_id = r.id`);
|
auth.roles r ON ur.role_id = r.id`);
|
||||||
|
|
||||||
|
|
||||||
// Tabla de Sesiones
|
// Tabla de Sesiones
|
||||||
export const sessions = authSchema.table(
|
export const sessions = authSchema.table(
|
||||||
'sessions',
|
'sessions',
|
||||||
@@ -104,9 +103,6 @@ export const sessions = authSchema.table(
|
|||||||
.notNull(),
|
.notNull(),
|
||||||
sessionToken: t.text('session_token').notNull(),
|
sessionToken: t.text('session_token').notNull(),
|
||||||
expiresAt: t.integer('expires_at').notNull(),
|
expiresAt: t.integer('expires_at').notNull(),
|
||||||
previousSessionToken: t.varchar('previous_session_token'),
|
|
||||||
lastRotatedAt: t.timestamp('last_rotated_at'),
|
|
||||||
|
|
||||||
...timestamps,
|
...timestamps,
|
||||||
},
|
},
|
||||||
(sessions) => ({
|
(sessions) => ({
|
||||||
@@ -114,6 +110,8 @@ export const sessions = authSchema.table(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//tabla de tokens de verificación
|
//tabla de tokens de verificación
|
||||||
export const verificationTokens = authSchema.table(
|
export const verificationTokens = authSchema.table(
|
||||||
'verificationToken',
|
'verificationToken',
|
||||||
|
|||||||
@@ -48,7 +48,8 @@ export const trainingSurveys = t.pgTable(
|
|||||||
{
|
{
|
||||||
// === 1. IDENTIFICADORES Y DATOS DE VISITA ===
|
// === 1. IDENTIFICADORES Y DATOS DE VISITA ===
|
||||||
id: t.serial('id').primaryKey(),
|
id: t.serial('id').primaryKey(),
|
||||||
coorFullName: t.text('coor_full_name').notNull(),
|
firstname: t.text('firstname').notNull(),
|
||||||
|
lastname: t.text('lastname').notNull(),
|
||||||
visitDate: t.timestamp('visit_date').notNull(),
|
visitDate: t.timestamp('visit_date').notNull(),
|
||||||
coorPhone: t.text('coor_phone'),
|
coorPhone: t.text('coor_phone'),
|
||||||
|
|
||||||
@@ -76,9 +77,8 @@ export const trainingSurveys = t.pgTable(
|
|||||||
.notNull()
|
.notNull()
|
||||||
.default(''),
|
.default(''),
|
||||||
productiveActivity: t.text('productive_activity').notNull(),
|
productiveActivity: t.text('productive_activity').notNull(),
|
||||||
productiveActivityOther: t.text('productive_activity_other').default(''),
|
ospRif: t.text('osp_rif').notNull(),
|
||||||
ospRif: t.text('osp_rif'),
|
ospName: t.text('osp_name').notNull(),
|
||||||
ospName: t.text('osp_name'),
|
|
||||||
companyConstitutionYear: t.integer('company_constitution_year').notNull(),
|
companyConstitutionYear: t.integer('company_constitution_year').notNull(),
|
||||||
currentStatus: t.text('current_status').notNull().default('ACTIVA'),
|
currentStatus: t.text('current_status').notNull().default('ACTIVA'),
|
||||||
infrastructureMt2: t.text('infrastructure_mt2').notNull().default(''),
|
infrastructureMt2: t.text('infrastructure_mt2').notNull().default(''),
|
||||||
@@ -98,13 +98,19 @@ export const trainingSurveys = t.pgTable(
|
|||||||
.text('commune_spokesperson_name')
|
.text('commune_spokesperson_name')
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(''),
|
.default(''),
|
||||||
communeSpokespersonCedula: t.text('commune_spokesperson_cedula'),
|
communeSpokespersonCedula: t
|
||||||
communeSpokespersonRif: t.text('commune_spokesperson_rif'),
|
.text('commune_spokesperson_cedula')
|
||||||
|
.notNull()
|
||||||
|
.default(''),
|
||||||
|
communeSpokespersonRif: t
|
||||||
|
.text('commune_spokesperson_rif')
|
||||||
|
.notNull()
|
||||||
|
.default(''),
|
||||||
communeSpokespersonPhone: t
|
communeSpokespersonPhone: t
|
||||||
.text('commune_spokesperson_phone')
|
.text('commune_spokesperson_phone')
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(''),
|
.default(''),
|
||||||
communeEmail: t.text('commune_email'),
|
communeEmail: t.text('commune_email').notNull().default(''),
|
||||||
communalCouncil: t.text('communal_council').notNull(),
|
communalCouncil: t.text('communal_council').notNull(),
|
||||||
siturCodeCommunalCouncil: t.text('situr_code_communal_council').notNull(),
|
siturCodeCommunalCouncil: t.text('situr_code_communal_council').notNull(),
|
||||||
communalCouncilRif: t.text('communal_council_rif').notNull().default(''),
|
communalCouncilRif: t.text('communal_council_rif').notNull().default(''),
|
||||||
@@ -112,10 +118,14 @@ export const trainingSurveys = t.pgTable(
|
|||||||
.text('communal_council_spokesperson_name')
|
.text('communal_council_spokesperson_name')
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(''),
|
.default(''),
|
||||||
communalCouncilSpokespersonCedula: t.text(
|
communalCouncilSpokespersonCedula: t
|
||||||
'communal_council_spokesperson_cedula',
|
.text('communal_council_spokesperson_cedula')
|
||||||
),
|
.notNull()
|
||||||
communalCouncilSpokespersonRif: t.text('communal_council_spokesperson_rif'),
|
.default(''),
|
||||||
|
communalCouncilSpokespersonRif: t
|
||||||
|
.text('communal_council_spokesperson_rif')
|
||||||
|
.notNull()
|
||||||
|
.default(''),
|
||||||
communalCouncilSpokespersonPhone: t
|
communalCouncilSpokespersonPhone: t
|
||||||
.text('communal_council_spokesperson_phone')
|
.text('communal_council_spokesperson_phone')
|
||||||
.notNull()
|
.notNull()
|
||||||
@@ -126,44 +136,22 @@ export const trainingSurveys = t.pgTable(
|
|||||||
.default(''),
|
.default(''),
|
||||||
ospResponsibleFullname: t.text('osp_responsible_fullname').notNull(),
|
ospResponsibleFullname: t.text('osp_responsible_fullname').notNull(),
|
||||||
ospResponsibleCedula: t.text('osp_responsible_cedula').notNull(),
|
ospResponsibleCedula: t.text('osp_responsible_cedula').notNull(),
|
||||||
ospResponsibleRif: t.text('osp_responsible_rif'),
|
ospResponsibleRif: t.text('osp_responsible_rif').notNull(),
|
||||||
civilState: t.text('civil_state'),
|
civilState: t.text('civil_state').notNull(),
|
||||||
ospResponsiblePhone: t.text('osp_responsible_phone').notNull(),
|
ospResponsiblePhone: t.text('osp_responsible_phone').notNull(),
|
||||||
ospResponsibleEmail: t.text('osp_responsible_email'),
|
ospResponsibleEmail: t.text('osp_responsible_email').notNull(),
|
||||||
familyBurden: t.integer('family_burden'),
|
familyBurden: t.integer('family_burden').notNull(),
|
||||||
numberOfChildren: t.integer('number_of_children'),
|
numberOfChildren: t.integer('number_of_children').notNull(),
|
||||||
generalObservations: t.text('general_observations'),
|
generalObservations: t.text('general_observations'),
|
||||||
|
|
||||||
// === 4. DATOS DE DISTRIBUCIÓN Y EXPORTACIÓN ===
|
|
||||||
internalDistributionZone: t.text('internal_distribution_zone'),
|
|
||||||
isExporting: t.boolean('is_exporting').notNull().default(false),
|
|
||||||
externalCountry: t.text('external_country'),
|
|
||||||
externalCity: t.text('external_city'),
|
|
||||||
externalDescription: t.text('external_description'),
|
|
||||||
externalQuantity: t.text('external_quantity'),
|
|
||||||
externalUnit: t.text('external_unit'),
|
|
||||||
|
|
||||||
// === 5. MANO DE OBRA ===
|
|
||||||
womenCount: t.integer('women_count').notNull().default(0),
|
|
||||||
menCount: t.integer('men_count').notNull().default(0),
|
|
||||||
|
|
||||||
// Fotos
|
|
||||||
photo1: t.text('photo1'),
|
photo1: t.text('photo1'),
|
||||||
photo2: t.text('photo2'),
|
photo2: t.text('photo2'),
|
||||||
photo3: t.text('photo3'),
|
photo3: t.text('photo3'),
|
||||||
// informacion del usuario que creo y actualizo el registro
|
|
||||||
createdBy: t
|
|
||||||
.integer('created_by')
|
|
||||||
.references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
updatedBy: t
|
|
||||||
.integer('updated_by')
|
|
||||||
.references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
...timestamps,
|
...timestamps,
|
||||||
},
|
},
|
||||||
(trainingSurveys) => ({
|
(trainingSurveys) => ({
|
||||||
trainingSurveysIndex: t
|
trainingSurveysIndex: t
|
||||||
.index('training_surveys_index_00')
|
.index('training_surveys_index_00')
|
||||||
.on(trainingSurveys.coorFullName),
|
.on(trainingSurveys.firstname),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
// api/src/feacture/auth/auth.controller.ts
|
// api/src/feacture/auth/auth.controller.ts
|
||||||
import { Public } from '@/common/decorators';
|
import { Public } from '@/common/decorators';
|
||||||
|
import { JwtRefreshGuard } from '@/common/guards/jwt-refresh.guard';
|
||||||
|
import { RefreshTokenDto } from '@/features/auth/dto/refresh-token.dto';
|
||||||
import { SignInUserDto } from '@/features/auth/dto/signIn-user.dto';
|
import { SignInUserDto } from '@/features/auth/dto/signIn-user.dto';
|
||||||
import { SignOutUserDto } from '@/features/auth/dto/signOut-user.dto';
|
import { SignOutUserDto } from '@/features/auth/dto/signOut-user.dto';
|
||||||
import { SingUpUserDto } from '@/features/auth/dto/signUp-user.dto';
|
import { SingUpUserDto } from '@/features/auth/dto/signUp-user.dto';
|
||||||
import { Body, Controller, HttpCode, Patch, Post } from '@nestjs/common';
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
HttpCode,
|
||||||
|
Patch,
|
||||||
|
Post,
|
||||||
|
Req,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) { }
|
constructor(private readonly authService: AuthService) {}
|
||||||
|
|
||||||
@Public()
|
@Public()
|
||||||
@HttpCode(200)
|
@HttpCode(200)
|
||||||
@@ -28,8 +39,6 @@ export class AuthController {
|
|||||||
return await this.authService.signIn(signInUserDto);
|
return await this.authService.signIn(signInUserDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Public()
|
|
||||||
@HttpCode(200)
|
|
||||||
@Post('sign-out')
|
@Post('sign-out')
|
||||||
//@RequirePermissions('auth:sign-out')
|
//@RequirePermissions('auth:sign-out')
|
||||||
async signOut(@Body() signOutUserDto: SignOutUserDto) {
|
async signOut(@Body() signOutUserDto: SignOutUserDto) {
|
||||||
@@ -49,11 +58,17 @@ export class AuthController {
|
|||||||
@Patch('refresh')
|
@Patch('refresh')
|
||||||
//@RequirePermissions('auth:refresh-token')
|
//@RequirePermissions('auth:refresh-token')
|
||||||
async refreshToken(@Body() refreshTokenDto: any) {
|
async refreshToken(@Body() refreshTokenDto: any) {
|
||||||
// console.log('REFRESCANDO');
|
|
||||||
// console.log(refreshTokenDto);
|
console.log('refreshTokenDto', refreshTokenDto);
|
||||||
// console.log('-----------');
|
|
||||||
|
const data = await this.authService.refreshToken(refreshTokenDto);
|
||||||
|
|
||||||
|
// console.log('data', data);
|
||||||
|
|
||||||
|
|
||||||
return await this.authService.refreshToken(refreshTokenDto);
|
if (!data) return null;
|
||||||
|
|
||||||
|
return {tokens: data}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Public()
|
// @Public()
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
import { JwtService, JwtSignOptions } from '@nestjs/jwt';
|
import { JwtService, JwtSignOptions } from '@nestjs/jwt';
|
||||||
import * as bcrypt from 'bcryptjs';
|
import * as bcrypt from 'bcryptjs';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { eq, or } from 'drizzle-orm';
|
import { and, eq, or } from 'drizzle-orm';
|
||||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||||
import * as schema from 'src/database/index';
|
import * as schema from 'src/database/index';
|
||||||
import { roles, sessions, users, usersRole } from 'src/database/index';
|
import { roles, sessions, users, usersRole } from 'src/database/index';
|
||||||
@@ -40,7 +40,7 @@ export class AuthService {
|
|||||||
private readonly config: ConfigService<Env>,
|
private readonly config: ConfigService<Env>,
|
||||||
@Inject(DRIZZLE_PROVIDER) private drizzle: NodePgDatabase<typeof schema>,
|
@Inject(DRIZZLE_PROVIDER) private drizzle: NodePgDatabase<typeof schema>,
|
||||||
private readonly mailService: MailService,
|
private readonly mailService: MailService,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
//Decode Tokens
|
//Decode Tokens
|
||||||
// Método para decodificar el token y obtener los datos completos
|
// Método para decodificar el token y obtener los datos completos
|
||||||
@@ -273,116 +273,48 @@ export class AuthService {
|
|||||||
|
|
||||||
//Refresh User Access Token
|
//Refresh User Access Token
|
||||||
async refreshToken(dto: RefreshTokenDto): Promise<RefreshTokenInterface> {
|
async refreshToken(dto: RefreshTokenDto): Promise<RefreshTokenInterface> {
|
||||||
const { refreshToken } = dto;
|
const secret = envs.refresh_token_secret;
|
||||||
|
const { user_id, token } = dto;
|
||||||
|
|
||||||
// 1. Validar firma del token (Crypto check)
|
console.log('secret', secret);
|
||||||
let payload: any;
|
console.log('refresh_token', token);
|
||||||
try {
|
|
||||||
payload = await this.jwtService.verifyAsync(refreshToken, {
|
|
||||||
secret: envs.refresh_token_secret,
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
throw new UnauthorizedException('Invalid Refresh Token Signature');
|
|
||||||
}
|
|
||||||
|
|
||||||
const userId = Number(payload.sub); // Es más seguro usar el ID del token que el del DTO
|
const validation = await this.jwtService.verifyAsync(token, {
|
||||||
|
secret,
|
||||||
|
});
|
||||||
|
|
||||||
// 2. Buscar la sesión por UserID (SIN filtrar por token todavía)
|
if (!validation) throw new UnauthorizedException('Invalid refresh token');
|
||||||
// Esto es clave: traemos la sesión para ver qué está pasando
|
|
||||||
const [currentSession] = await this.drizzle
|
const session = await this.drizzle
|
||||||
.select()
|
.select()
|
||||||
.from(sessions)
|
.from(sessions)
|
||||||
.where(eq(sessions.userId, userId));
|
.where(
|
||||||
|
and(eq(sessions.userId, user_id), eq(sessions.sessionToken, token)),
|
||||||
if (!currentSession) throw new NotFoundException('Session not found');
|
|
||||||
|
|
||||||
// CONFIGURACIÓN: Tiempo de gracia en milisegundos (ej: 15 segundos)
|
|
||||||
const GRACE_PERIOD_MS = 15000;
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// ESCENARIO A: Rotación Normal (El token coincide con el actual)
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
if (currentSession.sessionToken === refreshToken) {
|
|
||||||
const user = await this.findUserById(userId);
|
|
||||||
if (!user) throw new NotFoundException('User not found');
|
|
||||||
|
|
||||||
// Generar nuevos tokens (A -> B)
|
|
||||||
const tokensNew = await this.generateTokens(user);
|
|
||||||
const decodeAccess = this.decodeToken(tokensNew.access_token);
|
|
||||||
const decodeRefresh = this.decodeToken(tokensNew.refresh_token);
|
|
||||||
|
|
||||||
// Actualizamos DB guardando el token "viejo" como "previous"
|
|
||||||
await this.drizzle
|
|
||||||
.update(sessions)
|
|
||||||
.set({
|
|
||||||
sessionToken: tokensNew.refresh_token, // Nuevo (B)
|
|
||||||
previousSessionToken: refreshToken, // Viejo (A)
|
|
||||||
lastRotatedAt: new Date(), // Marca de tiempo
|
|
||||||
expiresAt: decodeRefresh.exp,
|
|
||||||
})
|
|
||||||
.where(eq(sessions.userId, userId));
|
|
||||||
|
|
||||||
return {
|
|
||||||
access_token: tokensNew.access_token,
|
|
||||||
access_expire_in: decodeAccess.exp,
|
|
||||||
refresh_token: tokensNew.refresh_token,
|
|
||||||
refresh_expire_in: decodeRefresh.exp,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// ESCENARIO B: Periodo de Gracia (Condición de Carrera)
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// El token no coincide con el actual, ¿pero coincide con el anterior?
|
|
||||||
const isPreviousToken =
|
|
||||||
currentSession.previousSessionToken === refreshToken;
|
|
||||||
|
|
||||||
// Calculamos cuánto tiempo ha pasado desde la rotación
|
|
||||||
const timeSinceRotation = currentSession.lastRotatedAt
|
|
||||||
? Date.now() - new Date(currentSession.lastRotatedAt).getTime()
|
|
||||||
: Infinity;
|
|
||||||
|
|
||||||
if (isPreviousToken && timeSinceRotation < GRACE_PERIOD_MS) {
|
|
||||||
// ¡Es una condición de carrera! El usuario envió 'A' pero ya rotamos a 'B'.
|
|
||||||
// Le devolvemos 'B' (el actual en DB) para que se sincronice.
|
|
||||||
|
|
||||||
const user = await this.findUserById(userId);
|
|
||||||
|
|
||||||
if (!user) throw new NotFoundException('User not found');
|
|
||||||
|
|
||||||
// Generamos un access token nuevo fresco (barato)
|
|
||||||
const accessTokenPayload = { sub: user.id, username: user.username };
|
|
||||||
const newAccessToken = await this.jwtService.signAsync(
|
|
||||||
accessTokenPayload,
|
|
||||||
{
|
|
||||||
secret: envs.access_token_secret,
|
|
||||||
expiresIn: envs.access_token_expiration,
|
|
||||||
} as JwtSignOptions,
|
|
||||||
);
|
);
|
||||||
const decodeAccess = this.decodeToken(newAccessToken);
|
|
||||||
|
|
||||||
// IMPORTANTE: Devolvemos el refresh token QUE YA ESTÁ EN LA BASE DE DATOS
|
// console.log(session.length);
|
||||||
// No generamos uno nuevo para no romper la cadena de la otra petición que ganó.
|
|
||||||
return {
|
|
||||||
access_token: newAccessToken,
|
|
||||||
access_expire_in: decodeAccess.exp,
|
|
||||||
refresh_token: currentSession.sessionToken!, // Devolvemos el token 'B'
|
|
||||||
refresh_expire_in: currentSession.expiresAt as number,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
if (session.length === 0) throw new NotFoundException('session not found');
|
||||||
// ESCENARIO C: Robo de Token (Reuse Detection)
|
const user = await this.findUserById(user_id);
|
||||||
// -------------------------------------------------------------------
|
if (!user) throw new NotFoundException('User not found');
|
||||||
// Si el token no es el actual, ni el anterior válido... ALGUIEN LO ROBÓ.
|
|
||||||
// O el usuario está intentando reusar un token muy viejo.
|
|
||||||
|
|
||||||
// Medida de seguridad: Borrar todas las sesiones del usuario
|
// Genera token
|
||||||
await this.drizzle.delete(sessions).where(eq(sessions.userId, userId));
|
const tokens = await this.generateTokens(user);
|
||||||
|
const decodeAccess = this.decodeToken(tokens.access_token);
|
||||||
|
const decodeRefresh = this.decodeToken(tokens.refresh_token);
|
||||||
|
|
||||||
throw new UnauthorizedException(
|
// Actualiza session
|
||||||
'Refresh token reuse detected. Access revoked.',
|
await this.drizzle
|
||||||
);
|
.update(sessions)
|
||||||
|
.set({ sessionToken: tokens.refresh_token, expiresAt: decodeRefresh.exp })
|
||||||
|
.where(eq(sessions.userId, user_id));
|
||||||
|
|
||||||
|
return {
|
||||||
|
access_token: tokens.access_token,
|
||||||
|
access_expire_in: decodeAccess.exp,
|
||||||
|
refresh_token: tokens.refresh_token,
|
||||||
|
refresh_expire_in: decodeRefresh.exp,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async singUp(createUserDto: SingUpUserDto): Promise<User> {
|
async singUp(createUserDto: SingUpUserDto): Promise<User> {
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ export class RefreshTokenDto {
|
|||||||
@IsString({
|
@IsString({
|
||||||
message: 'Refresh token must be a string',
|
message: 'Refresh token must be a string',
|
||||||
})
|
})
|
||||||
refreshToken: string;
|
token: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
userId: number;
|
user_id: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,23 @@
|
|||||||
import { Optional } from '@nestjs/common';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { Transform, Type } from 'class-transformer';
|
import { Transform, Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
IsArray,
|
IsArray,
|
||||||
|
IsBoolean,
|
||||||
IsDateString,
|
IsDateString,
|
||||||
IsEmail,
|
|
||||||
IsInt,
|
IsInt,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
ValidateIf,
|
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
|
||||||
export class CreateTrainingDto {
|
export class CreateTrainingDto {
|
||||||
// === 1. DATOS BÁSICOS ===
|
// === 1. DATOS BÁSICOS ===
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
coorFullName: string;
|
firstname: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
lastname: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsDateString()
|
@IsDateString()
|
||||||
@@ -28,11 +30,11 @@ export class CreateTrainingDto {
|
|||||||
|
|
||||||
// === 2. DATOS OSP ===
|
// === 2. DATOS OSP ===
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsOptional()
|
@IsString()
|
||||||
ospName: string;
|
ospName: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsOptional()
|
@IsString()
|
||||||
ospRif: string;
|
ospRif: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@@ -43,11 +45,6 @@ export class CreateTrainingDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
productiveActivity: string;
|
productiveActivity: string;
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
productiveActivityOther: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
currentStatus: string;
|
currentStatus: string;
|
||||||
@@ -78,14 +75,16 @@ export class CreateTrainingDto {
|
|||||||
structureType?: string;
|
structureType?: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsBoolean()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
hasTransport?: string;
|
@Transform(({ value }) => value === 'true' || value === true) // Convierte "false" -> false
|
||||||
|
hasTransport?: boolean;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsBoolean()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
isOpenSpace?: string;
|
@Transform(({ value }) => value === 'true' || value === true)
|
||||||
|
isOpenSpace?: boolean;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@@ -125,7 +124,6 @@ export class CreateTrainingDto {
|
|||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
|
||||||
ospResponsibleRif: string;
|
ospResponsibleRif: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@@ -133,25 +131,20 @@ export class CreateTrainingDto {
|
|||||||
ospResponsiblePhone: string;
|
ospResponsiblePhone: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsOptional()
|
@IsString()
|
||||||
@ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
|
ospResponsibleEmail: string;
|
||||||
@IsEmail()
|
|
||||||
ospResponsibleEmail?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
|
||||||
civilState: string;
|
civilState: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsOptional()
|
|
||||||
@Type(() => Number) // Convierte "3" -> 3
|
@Type(() => Number) // Convierte "3" -> 3
|
||||||
familyBurden: number;
|
familyBurden: number;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsOptional()
|
|
||||||
@Type(() => Number)
|
@Type(() => Number)
|
||||||
numberOfChildren: number;
|
numberOfChildren: number;
|
||||||
|
|
||||||
@@ -172,15 +165,21 @@ export class CreateTrainingDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
communeSpokespersonName: string;
|
communeSpokespersonName: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
communeSpokespersonCedula: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
communeSpokespersonRif: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
communeSpokespersonPhone: string;
|
communeSpokespersonPhone: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsOptional()
|
@IsString()
|
||||||
@ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
|
communeEmail: string;
|
||||||
@IsEmail()
|
|
||||||
communeEmail?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@@ -198,66 +197,25 @@ export class CreateTrainingDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
communalCouncilSpokespersonName: string;
|
communalCouncilSpokespersonName: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
communalCouncilSpokespersonCedula: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
communalCouncilSpokespersonRif: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
communalCouncilSpokespersonPhone: string;
|
communalCouncilSpokespersonPhone: string;
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
|
|
||||||
@IsEmail()
|
|
||||||
communalCouncilEmail?: string;
|
|
||||||
|
|
||||||
// === 6. DISTRIBUCIÓN Y EXPORTACIÓN ===
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
communalCouncilEmail: string;
|
||||||
internalDistributionZone?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
isExporting?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
externalCountry?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
// === 6. LISTAS (Arrays JSON) ===
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
externalCity?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
externalDescription?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
externalQuantity?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
externalUnit?: string;
|
|
||||||
|
|
||||||
// === 7. MANO DE OBRA ===
|
|
||||||
@ApiProperty()
|
|
||||||
@IsInt()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => Number)
|
|
||||||
womenCount?: number;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsInt()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => Number)
|
|
||||||
menCount?: number;
|
|
||||||
|
|
||||||
// === 8. LISTAS (Arrays JSON) ===
|
|
||||||
// Reciben un string JSON '[{...}]' y lo convierten a Objeto JS real
|
// Reciben un string JSON '[{...}]' y lo convierten a Objeto JS real
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@@ -305,11 +263,13 @@ export class CreateTrainingDto {
|
|||||||
})
|
})
|
||||||
productList?: any[];
|
productList?: any[];
|
||||||
|
|
||||||
//ubicacion
|
|
||||||
|
//ubicacion
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
state: string;
|
state: string;
|
||||||
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
municipality: string;
|
municipality: string;
|
||||||
@@ -317,19 +277,4 @@ export class CreateTrainingDto {
|
|||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsString()
|
@IsString()
|
||||||
parish: string;
|
parish: string;
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
photo1?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
photo2?: string;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
photo3?: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,13 @@ import {
|
|||||||
Patch,
|
Patch,
|
||||||
Post,
|
Post,
|
||||||
Query,
|
Query,
|
||||||
Req,
|
Res,
|
||||||
UploadedFiles,
|
UploadedFiles,
|
||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
|
StreamableFile,
|
||||||
|
Header
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
import { Readable } from 'stream';
|
||||||
import { FilesInterceptor } from '@nestjs/platform-express';
|
import { FilesInterceptor } from '@nestjs/platform-express';
|
||||||
import {
|
import {
|
||||||
ApiConsumes,
|
ApiConsumes,
|
||||||
@@ -24,30 +27,33 @@ import { CreateTrainingDto } from './dto/create-training.dto';
|
|||||||
import { TrainingStatisticsFilterDto } from './dto/training-statistics-filter.dto';
|
import { TrainingStatisticsFilterDto } from './dto/training-statistics-filter.dto';
|
||||||
import { UpdateTrainingDto } from './dto/update-training.dto';
|
import { UpdateTrainingDto } from './dto/update-training.dto';
|
||||||
import { TrainingService } from './training.service';
|
import { TrainingService } from './training.service';
|
||||||
|
import { Public } from '@/common/decorators';
|
||||||
|
|
||||||
@ApiTags('training')
|
@ApiTags('training')
|
||||||
@Controller('training')
|
@Controller('training')
|
||||||
export class TrainingController {
|
export class TrainingController {
|
||||||
constructor(private readonly trainingService: TrainingService) {}
|
constructor(private readonly trainingService: TrainingService) { }
|
||||||
|
|
||||||
// @Public()
|
// export training with excel
|
||||||
// @Get('export/:id')
|
@Public()
|
||||||
// @ApiOperation({ summary: 'Export training template' })
|
@Get('export/:id')
|
||||||
// @ApiResponse({
|
@ApiOperation({ summary: 'Export training with excel' })
|
||||||
// status: 200,
|
@ApiResponse({
|
||||||
// description: 'Return training template.',
|
status: 200,
|
||||||
// content: { 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { schema: { type: 'string', format: 'binary' } } }
|
description: 'Return training with excel.',
|
||||||
// })
|
content: { 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { schema: { type: 'string', format: 'binary' } } }
|
||||||
// @Header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
})
|
||||||
// @Header('Content-Disposition', 'attachment; filename=export_osp.xlsx')
|
@Header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||||
// async exportTemplate(@Param('id') id: string) {
|
@Header('Content-Disposition', 'attachment; filename=export_osp.xlsx')
|
||||||
// if (!Number(id)) {
|
async exportTemplate(@Param('id') id: string) {
|
||||||
// throw new Error('ID is required');
|
if (!Number(id)) {
|
||||||
// }
|
throw new Error('ID is required');
|
||||||
// const data = await this.trainingService.exportTemplate(Number(id));
|
}
|
||||||
// return new StreamableFile(data);
|
const data = await this.trainingService.exportTemplate(Number(id));
|
||||||
// }
|
return new StreamableFile(Readable.from([data]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// get all training records
|
||||||
@Get()
|
@Get()
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: 'Get all training records with pagination and filters',
|
summary: 'Get all training records with pagination and filters',
|
||||||
@@ -65,6 +71,7 @@ export class TrainingController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get training statistics
|
||||||
@Get('statistics')
|
@Get('statistics')
|
||||||
@ApiOperation({ summary: 'Get training statistics' })
|
@ApiOperation({ summary: 'Get training statistics' })
|
||||||
@ApiResponse({ status: 200, description: 'Return training statistics.' })
|
@ApiResponse({ status: 200, description: 'Return training statistics.' })
|
||||||
@@ -73,6 +80,7 @@ export class TrainingController {
|
|||||||
return { message: 'Training statistics fetched successfully', data };
|
return { message: 'Training statistics fetched successfully', data };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get training record by id
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
@ApiOperation({ summary: 'Get a training record by ID' })
|
@ApiOperation({ summary: 'Get a training record by ID' })
|
||||||
@ApiResponse({ status: 200, description: 'Return the training record.' })
|
@ApiResponse({ status: 200, description: 'Return the training record.' })
|
||||||
@@ -82,6 +90,7 @@ export class TrainingController {
|
|||||||
return { message: 'Training record fetched successfully', data };
|
return { message: 'Training record fetched successfully', data };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create training record
|
||||||
@Post()
|
@Post()
|
||||||
@UseInterceptors(FilesInterceptor('files', 3))
|
@UseInterceptors(FilesInterceptor('files', 3))
|
||||||
@ApiConsumes('multipart/form-data')
|
@ApiConsumes('multipart/form-data')
|
||||||
@@ -91,19 +100,14 @@ export class TrainingController {
|
|||||||
description: 'Training record created successfully.',
|
description: 'Training record created successfully.',
|
||||||
})
|
})
|
||||||
async create(
|
async create(
|
||||||
@Req() req: Request,
|
|
||||||
@Body() createTrainingDto: CreateTrainingDto,
|
@Body() createTrainingDto: CreateTrainingDto,
|
||||||
@UploadedFiles(ImageProcessingPipe) files: Express.Multer.File[],
|
@UploadedFiles(ImageProcessingPipe) files: Express.Multer.File[],
|
||||||
) {
|
) {
|
||||||
const userId = (req as any).user?.id;
|
const data = await this.trainingService.create(createTrainingDto, files);
|
||||||
const data = await this.trainingService.create(
|
|
||||||
createTrainingDto,
|
|
||||||
files,
|
|
||||||
userId,
|
|
||||||
);
|
|
||||||
return { message: 'Training record created successfully', data };
|
return { message: 'Training record created successfully', data };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update training record
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
@UseInterceptors(FilesInterceptor('files', 3))
|
@UseInterceptors(FilesInterceptor('files', 3))
|
||||||
@ApiConsumes('multipart/form-data')
|
@ApiConsumes('multipart/form-data')
|
||||||
@@ -114,21 +118,19 @@ export class TrainingController {
|
|||||||
})
|
})
|
||||||
@ApiResponse({ status: 404, description: 'Training record not found.' })
|
@ApiResponse({ status: 404, description: 'Training record not found.' })
|
||||||
async update(
|
async update(
|
||||||
@Req() req: Request,
|
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() updateTrainingDto: UpdateTrainingDto,
|
@Body() updateTrainingDto: UpdateTrainingDto,
|
||||||
@UploadedFiles(ImageProcessingPipe) files: Express.Multer.File[],
|
@UploadedFiles(ImageProcessingPipe) files: Express.Multer.File[],
|
||||||
) {
|
) {
|
||||||
const userId = (req as any).user?.id;
|
|
||||||
const data = await this.trainingService.update(
|
const data = await this.trainingService.update(
|
||||||
+id,
|
+id,
|
||||||
updateTrainingDto,
|
updateTrainingDto,
|
||||||
files,
|
files,
|
||||||
userId,
|
|
||||||
);
|
);
|
||||||
return { message: 'Training record updated successfully', data };
|
return { message: 'Training record updated successfully', data };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delete training record
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@ApiOperation({ summary: 'Delete a training record' })
|
@ApiOperation({ summary: 'Delete a training record' })
|
||||||
@ApiResponse({
|
@ApiResponse({
|
||||||
|
|||||||
@@ -1,21 +1,24 @@
|
|||||||
import { MinioService } from '@/common/minio/minio.service';
|
import { HttpException, HttpStatus, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
|
import { and, eq, getTableColumns, gte, ilike, lte, or, SQL, sql } from 'drizzle-orm';
|
||||||
import { and, eq, gte, ilike, lte, or, SQL, sql } from 'drizzle-orm';
|
|
||||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
import { DRIZZLE_PROVIDER } from 'src/database/drizzle-provider';
|
import { DRIZZLE_PROVIDER } from 'src/database/drizzle-provider';
|
||||||
import * as schema from 'src/database/index';
|
import * as schema from 'src/database/index';
|
||||||
import { states, trainingSurveys } from 'src/database/index';
|
import { municipalities, parishes, states, trainingSurveys } from 'src/database/index';
|
||||||
|
// import XlsxPopulate from 'xlsx-populate';
|
||||||
|
import ExcelJS from 'exceljs';
|
||||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||||
import { CreateTrainingDto } from './dto/create-training.dto';
|
import { CreateTrainingDto } from './dto/create-training.dto';
|
||||||
import { TrainingStatisticsFilterDto } from './dto/training-statistics-filter.dto';
|
import { TrainingStatisticsFilterDto } from './dto/training-statistics-filter.dto';
|
||||||
import { UpdateTrainingDto } from './dto/update-training.dto';
|
import { UpdateTrainingDto } from './dto/update-training.dto';
|
||||||
|
import sharp from 'sharp';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TrainingService {
|
export class TrainingService {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DRIZZLE_PROVIDER) private drizzle: NodePgDatabase<typeof schema>,
|
@Inject(DRIZZLE_PROVIDER) private drizzle: NodePgDatabase<typeof schema>,
|
||||||
private readonly minioService: MinioService,
|
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async findAll(paginationDto?: PaginationDto) {
|
async findAll(paginationDto?: PaginationDto) {
|
||||||
@@ -107,7 +110,17 @@ export class TrainingService {
|
|||||||
// 2. Total Productores (Columna plana que mantuviste)
|
// 2. Total Productores (Columna plana que mantuviste)
|
||||||
this.drizzle
|
this.drizzle
|
||||||
.select({
|
.select({
|
||||||
sum: sql<number>`SUM(${trainingSurveys.womenCount} + ${trainingSurveys.menCount})`,
|
sum: sql<number>`
|
||||||
|
SUM(
|
||||||
|
(
|
||||||
|
SELECT SUM(
|
||||||
|
COALESCE((item->>'menCount')::int, 0) +
|
||||||
|
COALESCE((item->>'womenCount')::int, 0)
|
||||||
|
)
|
||||||
|
FROM jsonb_array_elements(${trainingSurveys.productList}) as item
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`,
|
||||||
})
|
})
|
||||||
.from(trainingSurveys)
|
.from(trainingSurveys)
|
||||||
.where(whereCondition),
|
.where(whereCondition),
|
||||||
@@ -205,9 +218,17 @@ export class TrainingService {
|
|||||||
|
|
||||||
async findOne(id: number) {
|
async findOne(id: number) {
|
||||||
const find = await this.drizzle
|
const find = await this.drizzle
|
||||||
.select()
|
.select({
|
||||||
|
...getTableColumns(trainingSurveys),
|
||||||
|
stateName: states.name,
|
||||||
|
municipalityName: municipalities.name,
|
||||||
|
parishName: parishes.name,
|
||||||
|
})
|
||||||
.from(trainingSurveys)
|
.from(trainingSurveys)
|
||||||
.where(eq(trainingSurveys.id, id));
|
.leftJoin(states, eq(trainingSurveys.state, states.id))
|
||||||
|
.leftJoin(municipalities, eq(trainingSurveys.municipality, municipalities.id))
|
||||||
|
.leftJoin(parishes, eq(trainingSurveys.parish, parishes.id))
|
||||||
|
.where(eq(trainingSurveys.id, id))
|
||||||
|
|
||||||
if (find.length === 0) {
|
if (find.length === 0) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@@ -222,54 +243,50 @@ export class TrainingService {
|
|||||||
private async saveFiles(files: Express.Multer.File[]): Promise<string[]> {
|
private async saveFiles(files: Express.Multer.File[]): Promise<string[]> {
|
||||||
if (!files || files.length === 0) return [];
|
if (!files || files.length === 0) return [];
|
||||||
|
|
||||||
|
const uploadDir = './uploads/training';
|
||||||
|
if (!fs.existsSync(uploadDir)) {
|
||||||
|
fs.mkdirSync(uploadDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
const savedPaths: string[] = [];
|
const savedPaths: string[] = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const objectName = await this.minioService.upload(file, 'training');
|
const fileName = `${Date.now()}-${Math.round(Math.random() * 1e9)}.png`;
|
||||||
const fileUrl = this.minioService.getPublicUrl(objectName);
|
const filePath = path.join(uploadDir, fileName);
|
||||||
savedPaths.push(fileUrl);
|
|
||||||
|
// Convertir a PNG usando sharp antes de guardar
|
||||||
|
await sharp(file.buffer)
|
||||||
|
.png()
|
||||||
|
.toFile(filePath);
|
||||||
|
|
||||||
|
savedPaths.push(`/assets/training/${fileName}`);
|
||||||
}
|
}
|
||||||
return savedPaths;
|
return savedPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async deleteFile(fileUrl: string) {
|
private deleteFile(assetPath: string) {
|
||||||
if (!fileUrl) return;
|
if (!assetPath) return;
|
||||||
|
// Map /assets/training/filename.webp back to ./uploads/training/filename.webp
|
||||||
|
const relativePath = assetPath.replace('/assets/training/', '');
|
||||||
|
const fullPath = path.join('./uploads/training', relativePath);
|
||||||
|
|
||||||
try {
|
if (fs.existsSync(fullPath)) {
|
||||||
// If it's a full URL, we need to extract the part after the bucket name
|
try {
|
||||||
if (fileUrl.startsWith('http')) {
|
fs.unlinkSync(fullPath);
|
||||||
const url = new URL(fileUrl);
|
} catch (err) {
|
||||||
const pathname = url.pathname; // /bucket/folder/filename
|
console.error(`Error deleting file ${fullPath}:`, err);
|
||||||
const parts = pathname.split('/').filter(Boolean); // ['bucket', 'folder', 'filename']
|
|
||||||
|
|
||||||
// The first part is the bucket name, the rest is the object name
|
|
||||||
if (parts.length >= 2) {
|
|
||||||
const objectName = parts.slice(1).join('/');
|
|
||||||
await this.minioService.delete(objectName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's not a URL or doesn't match the expected format, pass it as is
|
|
||||||
await this.minioService.delete(fileUrl);
|
|
||||||
} catch (error) {
|
|
||||||
// Fallback if URL parsing fails
|
|
||||||
await this.minioService.delete(fileUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(
|
async create(
|
||||||
createTrainingDto: CreateTrainingDto,
|
createTrainingDto: CreateTrainingDto,
|
||||||
files: Express.Multer.File[],
|
files: Express.Multer.File[],
|
||||||
userId: number,
|
|
||||||
) {
|
) {
|
||||||
// 1. Guardar fotos
|
// 1. Guardar fotos
|
||||||
|
|
||||||
const photoPaths = await this.saveFiles(files);
|
const photoPaths = await this.saveFiles(files);
|
||||||
|
|
||||||
// 2. Extraer solo visitDate para formatearlo.
|
// 2. Extraer solo visitDate para formatearlo.
|
||||||
// Ya NO extraemos state, municipality, etc. porque no vienen en el DTO.
|
const { visitDate, state, municipality, parish, ...rest } = createTrainingDto;
|
||||||
const { visitDate, state, municipality, parish, productiveActivityOther, ...rest } =
|
|
||||||
createTrainingDto;
|
|
||||||
|
|
||||||
const [newRecord] = await this.drizzle
|
const [newRecord] = await this.drizzle
|
||||||
.insert(trainingSurveys)
|
.insert(trainingSurveys)
|
||||||
@@ -280,9 +297,6 @@ export class TrainingService {
|
|||||||
// Conversión de fecha
|
// Conversión de fecha
|
||||||
visitDate: new Date(visitDate),
|
visitDate: new Date(visitDate),
|
||||||
|
|
||||||
// Borra las tildes y cambia el texto a mayusculas
|
|
||||||
productiveActivityOther: productiveActivityOther.toUpperCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
|
||||||
|
|
||||||
// 3. Asignar fotos de forma segura
|
// 3. Asignar fotos de forma segura
|
||||||
photo1: photoPaths[0] ?? null,
|
photo1: photoPaths[0] ?? null,
|
||||||
photo2: photoPaths[1] ?? null,
|
photo2: photoPaths[1] ?? null,
|
||||||
@@ -290,11 +304,6 @@ export class TrainingService {
|
|||||||
state: Number(state) ?? null,
|
state: Number(state) ?? null,
|
||||||
municipality: Number(municipality) ?? null,
|
municipality: Number(municipality) ?? null,
|
||||||
parish: Number(parish) ?? null,
|
parish: Number(parish) ?? null,
|
||||||
hasTransport: rest.hasTransport === 'true' ? true : false,
|
|
||||||
isOpenSpace: rest.isOpenSpace === 'true' ? true : false,
|
|
||||||
isExporting: rest.isExporting === 'true' ? true : false,
|
|
||||||
createdBy: userId,
|
|
||||||
updatedBy: userId,
|
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
@@ -305,68 +314,41 @@ export class TrainingService {
|
|||||||
id: number,
|
id: number,
|
||||||
updateTrainingDto: UpdateTrainingDto,
|
updateTrainingDto: UpdateTrainingDto,
|
||||||
files: Express.Multer.File[],
|
files: Express.Multer.File[],
|
||||||
userId: number,
|
|
||||||
) {
|
) {
|
||||||
const currentRecord = await this.findOne(id);
|
const currentRecord = await this.findOne(id);
|
||||||
const photoFields = ['photo1', 'photo2', 'photo3'] as const;
|
|
||||||
|
|
||||||
// 1. Guardar fotos nuevas en MinIO
|
const photoPaths = await this.saveFiles(files);
|
||||||
const newFilePaths = await this.saveFiles(files);
|
|
||||||
|
|
||||||
const updateData: any = { ...updateTrainingDto };
|
const updateData: any = { ...updateTrainingDto };
|
||||||
|
|
||||||
// 2. Determinar el estado final de las fotos (diff)
|
// Handle photo updates/removals
|
||||||
// - Si el DTO tiene un valor (URL existente o ''), lo usamos.
|
const photoFields = ['photo1', 'photo2', 'photo3'] as const;
|
||||||
// - Si el DTO no tiene el campo (undefined), mantenemos el de la DB.
|
|
||||||
const finalPhotos: (string | null)[] = photoFields.map((field) => {
|
|
||||||
const dtoValue = updateData[field];
|
|
||||||
if (dtoValue !== undefined) {
|
|
||||||
return dtoValue === '' ? null : dtoValue;
|
|
||||||
}
|
|
||||||
return currentRecord[field];
|
|
||||||
});
|
|
||||||
|
|
||||||
// 3. Asignar los nuevos paths subidos a los slots que quedaron vacíos
|
// 1. If we have NEW files, they replace any old files or occupy empty slots
|
||||||
if (newFilePaths.length > 0) {
|
if (photoPaths.length > 0) {
|
||||||
let newIdx = 0;
|
photoPaths.forEach((newPath, idx) => {
|
||||||
for (let i = 0; i < 3 && newIdx < newFilePaths.length; i++) {
|
const fieldName = photoFields[idx];
|
||||||
if (!finalPhotos[i]) {
|
const oldPath = currentRecord[fieldName];
|
||||||
finalPhotos[i] = newFilePaths[newIdx];
|
if (oldPath && oldPath !== newPath) {
|
||||||
newIdx++;
|
this.deleteFile(oldPath);
|
||||||
}
|
}
|
||||||
}
|
updateData[fieldName] = newPath;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. LIMPIEZA: Borrar de MinIO los archivos que ya no están en ningún slot
|
// 2. If the user explicitly cleared a photo field (updateData.photoX === '')
|
||||||
const oldPhotos = photoFields
|
photoFields.forEach((field) => {
|
||||||
.map((f) => currentRecord[f])
|
if (updateData[field] === '') {
|
||||||
.filter((p): p is string => Boolean(p));
|
const oldPath = currentRecord[field];
|
||||||
const newPhotosSet = new Set(finalPhotos.filter(Boolean));
|
if (oldPath) this.deleteFile(oldPath);
|
||||||
|
updateData[field] = null; // Set to null in DB
|
||||||
for (const oldPath of oldPhotos) {
|
|
||||||
if (!newPhotosSet.has(oldPath)) {
|
|
||||||
await this.deleteFile(oldPath);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// 5. Preparar datos finales para la DB
|
|
||||||
updateData.photo1 = finalPhotos[0];
|
|
||||||
updateData.photo2 = finalPhotos[1];
|
|
||||||
updateData.photo3 = finalPhotos[2];
|
|
||||||
|
|
||||||
if (updateTrainingDto.visitDate) {
|
if (updateTrainingDto.visitDate) {
|
||||||
updateData.visitDate = new Date(updateTrainingDto.visitDate);
|
updateData.visitDate = new Date(updateTrainingDto.visitDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// actualizamos el id del usuario que actualizo el registro
|
|
||||||
updateData.updatedBy = userId;
|
|
||||||
updateData.hasTransport =
|
|
||||||
updateTrainingDto.hasTransport === 'true' ? true : false;
|
|
||||||
updateData.isOpenSpace =
|
|
||||||
updateTrainingDto.isOpenSpace === 'true' ? true : false;
|
|
||||||
updateData.isExporting =
|
|
||||||
updateTrainingDto.isExporting === 'true' ? true : false;
|
|
||||||
|
|
||||||
const [updatedRecord] = await this.drizzle
|
const [updatedRecord] = await this.drizzle
|
||||||
.update(trainingSurveys)
|
.update(trainingSurveys)
|
||||||
.set(updateData)
|
.set(updateData)
|
||||||
@@ -380,9 +362,9 @@ export class TrainingService {
|
|||||||
const record = await this.findOne(id);
|
const record = await this.findOne(id);
|
||||||
|
|
||||||
// Delete associated files
|
// Delete associated files
|
||||||
if (record.photo1) await this.deleteFile(record.photo1);
|
if (record.photo1) this.deleteFile(record.photo1);
|
||||||
if (record.photo2) await this.deleteFile(record.photo2);
|
if (record.photo2) this.deleteFile(record.photo2);
|
||||||
if (record.photo3) await this.deleteFile(record.photo3);
|
if (record.photo3) this.deleteFile(record.photo3);
|
||||||
|
|
||||||
const [deletedRecord] = await this.drizzle
|
const [deletedRecord] = await this.drizzle
|
||||||
.delete(trainingSurveys)
|
.delete(trainingSurveys)
|
||||||
@@ -395,296 +377,163 @@ export class TrainingService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// async exportTemplate() {
|
async exportTemplate(id: number) {
|
||||||
|
// Validar que el registro exista
|
||||||
|
const record = await this.findOne(id);
|
||||||
|
if (!record) throw new NotFoundException(`No se encontró el registro`);
|
||||||
|
|
||||||
// const templatePath = path.join(
|
// Formatear fecha y hora
|
||||||
// __dirname,
|
const dateObj = new Date(record.visitDate);
|
||||||
// 'export_template',
|
const fechaFormateada = dateObj.toLocaleDateString('es-ES');
|
||||||
// 'excel.osp.xlsx',
|
const horaFormateada = dateObj.toLocaleTimeString('es-ES', {
|
||||||
// );
|
hour: '2-digit',
|
||||||
// const templateBuffer = fs.readFileSync(templatePath);
|
minute: '2-digit',
|
||||||
|
});
|
||||||
|
|
||||||
// const workbook: any = await XlsxPopulate.fromDataAsync(templateBuffer);
|
// Ruta de la plantilla
|
||||||
// const sheet = workbook.sheet(0);
|
const templatePath = path.join(
|
||||||
|
__dirname,
|
||||||
|
'export_template',
|
||||||
|
'excel.osp.xlsx',
|
||||||
|
);
|
||||||
|
|
||||||
// const records = await this.drizzle
|
// Cargar la plantilla con ExcelJS
|
||||||
// .select({
|
const workbook = new ExcelJS.Workbook();
|
||||||
// coorFullName: trainingSurveys.coorFullName,
|
await workbook.xlsx.readFile(templatePath);
|
||||||
// visitDate: trainingSurveys.visitDate,
|
const worksheet = workbook.getWorksheet(1); // Usar la primera hoja
|
||||||
// stateName: states.name,
|
|
||||||
// municipalityName: municipalities.name,
|
|
||||||
// parishName: parishes.name,
|
|
||||||
// communeName: trainingSurveys.communeName,
|
|
||||||
// siturCodeCommune: trainingSurveys.siturCodeCommune,
|
|
||||||
// communalCouncil: trainingSurveys.communalCouncil,
|
|
||||||
// siturCodeCommunalCouncil: trainingSurveys.siturCodeCommunalCouncil,
|
|
||||||
// productiveActivity: trainingSurveys.productiveActivity,
|
|
||||||
// ospName: trainingSurveys.ospName,
|
|
||||||
// ospAddress: trainingSurveys.ospAddress,
|
|
||||||
// ospRif: trainingSurveys.ospRif,
|
|
||||||
// ospType: trainingSurveys.ospType,
|
|
||||||
// currentStatus: trainingSurveys.currentStatus,
|
|
||||||
// companyConstitutionYear: trainingSurveys.companyConstitutionYear,
|
|
||||||
// ospResponsibleFullname: trainingSurveys.ospResponsibleFullname,
|
|
||||||
// ospResponsibleCedula: trainingSurveys.ospResponsibleCedula,
|
|
||||||
// ospResponsibleRif: trainingSurveys.ospResponsibleRif,
|
|
||||||
// ospResponsiblePhone: trainingSurveys.ospResponsiblePhone,
|
|
||||||
// ospResponsibleEmail: trainingSurveys.ospResponsibleEmail,
|
|
||||||
// civilState: trainingSurveys.civilState,
|
|
||||||
// familyBurden: trainingSurveys.familyBurden,
|
|
||||||
// numberOfChildren: trainingSurveys.numberOfChildren,
|
|
||||||
// generalObservations: trainingSurveys.generalObservations,
|
|
||||||
// paralysisReason: trainingSurveys.paralysisReason,
|
|
||||||
// productList: trainingSurveys.productList,
|
|
||||||
// infrastructureMt2: trainingSurveys.infrastructureMt2,
|
|
||||||
// photo1: trainingSurveys.photo1,
|
|
||||||
// photo2: trainingSurveys.photo2,
|
|
||||||
// photo3: trainingSurveys.photo3,
|
|
||||||
// })
|
|
||||||
// .from(trainingSurveys)
|
|
||||||
// .leftJoin(states, eq(trainingSurveys.state, states.id))
|
|
||||||
// .leftJoin(
|
|
||||||
// municipalities,
|
|
||||||
// eq(trainingSurveys.municipality, municipalities.id),
|
|
||||||
// )
|
|
||||||
// .leftJoin(parishes, eq(trainingSurveys.parish, parishes.id))
|
|
||||||
// .execute();
|
|
||||||
|
|
||||||
// let currentRow = 2;
|
if (!worksheet) {
|
||||||
|
throw new Error('No se pudo encontrar la hoja de trabajo en la plantilla');
|
||||||
|
}
|
||||||
|
|
||||||
// for (const record of records) {
|
// Llenar los datos principales
|
||||||
// const date = new Date(record.visitDate);
|
worksheet.getCell('A6').value = record.productiveSector;
|
||||||
// const dateStr = date.toLocaleDateString('es-VE');
|
worksheet.getCell('B8').value = record.stateName;
|
||||||
// const timeStr = date.toLocaleTimeString('es-VE');
|
worksheet.getCell('E8').value = record.municipalityName;
|
||||||
|
worksheet.getCell('B9').value = record.parishName;
|
||||||
|
worksheet.getCell('D6').value = record.ospName;
|
||||||
|
worksheet.getCell('L5').value = fechaFormateada;
|
||||||
|
worksheet.getCell('L6').value = horaFormateada;
|
||||||
|
worksheet.getCell('B10').value = record.ospAddress;
|
||||||
|
worksheet.getCell('C11').value = record.communeEmail;
|
||||||
|
worksheet.getCell('C12').value = record.communeSpokespersonName;
|
||||||
|
worksheet.getCell('G11').value = record.communeRif;
|
||||||
|
worksheet.getCell('G12').value = record.communeSpokespersonPhone;
|
||||||
|
worksheet.getCell('C13').value = record.siturCodeCommune;
|
||||||
|
worksheet.getCell('G13').value = record.siturCodeCommunalCouncil;
|
||||||
|
worksheet.getCell('G14').value = record.communalCouncilRif;
|
||||||
|
worksheet.getCell('C15').value = record.communalCouncilSpokespersonName;
|
||||||
|
worksheet.getCell('G15').value = record.communalCouncilSpokespersonPhone;
|
||||||
|
worksheet.getCell('C16').value = record.ospType;
|
||||||
|
worksheet.getCell('C17').value = record.ospName;
|
||||||
|
worksheet.getCell('C18').value = record.productiveActivity;
|
||||||
|
worksheet.getCell('C19').value = 'Proveedores';
|
||||||
|
worksheet.getCell('C20').value = record.companyConstitutionYear;
|
||||||
|
worksheet.getCell('C21').value = record.infrastructureMt2;
|
||||||
|
worksheet.getCell('G17').value = record.ospRif;
|
||||||
|
|
||||||
// sheet.cell(`A${currentRow}`).value(record.coorFullName);
|
worksheet.getCell(record.hasTransport === true ? 'J19' : 'L19').value = 'X';
|
||||||
// sheet.cell(`C${currentRow}`).value(dateStr);
|
worksheet.getCell(record.structureType === 'CASA' ? 'J20' : 'L20').value =
|
||||||
// sheet.cell(`D${currentRow}`).value(timeStr);
|
'X';
|
||||||
// sheet.cell(`E${currentRow}`).value(record.stateName || '');
|
worksheet.getCell(record.isOpenSpace === true ? 'J21' : 'L21').value = 'X';
|
||||||
// sheet.cell(`F${currentRow}`).value(record.municipalityName || '');
|
|
||||||
// sheet.cell(`G${currentRow}`).value(record.parishName || '');
|
|
||||||
// sheet.cell(`H${currentRow}`).value(record.communeName);
|
|
||||||
// sheet.cell(`I${currentRow}`).value(record.siturCodeCommune);
|
|
||||||
// sheet.cell(`J${currentRow}`).value(record.communalCouncil);
|
|
||||||
// sheet.cell(`K${currentRow}`).value(record.siturCodeCommunalCouncil);
|
|
||||||
// sheet.cell(`L${currentRow}`).value(record.productiveActivity);
|
|
||||||
// sheet.cell(`M${currentRow}`).value(''); // requerimiento financiero description
|
|
||||||
// sheet.cell(`N${currentRow}`).value(record.ospName);
|
|
||||||
// sheet.cell(`O${currentRow}`).value(record.ospAddress);
|
|
||||||
// sheet.cell(`P${currentRow}`).value(record.ospRif);
|
|
||||||
// sheet.cell(`Q${currentRow}`).value(record.ospType);
|
|
||||||
// sheet.cell(`R${currentRow}`).value(record.currentStatus);
|
|
||||||
// sheet.cell(`S${currentRow}`).value(record.companyConstitutionYear);
|
|
||||||
|
|
||||||
// const products = (record.productList as any[]) || [];
|
worksheet.getCell('A24').value = record.ospResponsibleFullname;
|
||||||
// const totalProducers = products.reduce(
|
worksheet.getCell('C24').value = record.ospResponsibleCedula;
|
||||||
// (sum, p) =>
|
worksheet.getCell('E24').value = record.ospResponsiblePhone;
|
||||||
// sum + (Number(p.menCount) || 0) + (Number(p.womenCount) || 0),
|
|
||||||
// 0,
|
|
||||||
// );
|
|
||||||
// const productsDesc = products.map((p) => p.name).join(', ');
|
|
||||||
|
|
||||||
// sheet.cell(`T${currentRow}`).value(totalProducers);
|
worksheet.getCell('J24').value = 'N Femenino'; // Placeholder si no hay dato
|
||||||
// sheet.cell(`U${currentRow}`).value(productsDesc);
|
worksheet.getCell('L24').value = 'N Masculino'; // Placeholder si no hay dato
|
||||||
// sheet.cell(`V${currentRow}`).value(record.infrastructureMt2);
|
|
||||||
// sheet.cell(`W${currentRow}`).value('');
|
|
||||||
// sheet.cell(`X${currentRow}`).value(record.paralysisReason || '');
|
|
||||||
// sheet.cell(`Y${currentRow}`).value(record.ospResponsibleFullname);
|
|
||||||
// sheet.cell(`Z${currentRow}`).value(record.ospResponsibleCedula);
|
|
||||||
// sheet.cell(`AA${currentRow}`).value(record.ospResponsibleRif);
|
|
||||||
// sheet.cell(`AB${currentRow}`).value(record.ospResponsiblePhone);
|
|
||||||
// sheet.cell(`AC${currentRow}`).value(record.ospResponsibleEmail);
|
|
||||||
// sheet.cell(`AD${currentRow}`).value(record.civilState);
|
|
||||||
// sheet.cell(`AE${currentRow}`).value(record.familyBurden);
|
|
||||||
// sheet.cell(`AF${currentRow}`).value(record.numberOfChildren);
|
|
||||||
// sheet.cell(`AG${currentRow}`).value(record.generalObservations || '');
|
|
||||||
|
|
||||||
// sheet.cell(`AH${currentRow}`).value(record.photo1 || '');
|
// const photo1 = record.photo1;
|
||||||
// sheet.cell(`AI${currentRow}`).value(record.photo2 || '');
|
// const photo2 = record.photo2;
|
||||||
// sheet.cell(`AJ${currentRow}`).value(record.photo3 || '');
|
// const photo3 = record.photo3;
|
||||||
|
|
||||||
// currentRow++;
|
if (record.photo1) {
|
||||||
// }
|
const image = record.photo1.slice(17);
|
||||||
|
const extension = image.split('.')[1];
|
||||||
|
|
||||||
// return await workbook.outputAsync();
|
// Validar que sea una imagen png, gif o jpeg
|
||||||
// }
|
if (extension === 'png' || extension === 'gif' || extension === 'jpeg') {
|
||||||
|
// Ruta de la imagen
|
||||||
|
const imagePath = path.join(
|
||||||
|
__dirname,
|
||||||
|
'../../../',
|
||||||
|
`uploads/training/${image}`,
|
||||||
|
);
|
||||||
|
|
||||||
// async exportTemplate(id: number) {
|
// Add an image to the workbook from a file buffer
|
||||||
// // Validar que el registro exista
|
const logoId = workbook.addImage({
|
||||||
// const exist = await this.findOne(id);
|
filename: imagePath,
|
||||||
// if (!exist) throw new NotFoundException(`No se encontro el registro`);
|
extension: extension,
|
||||||
|
});
|
||||||
|
|
||||||
// // Obtener los datos del registro
|
// Anchor the image to a specific cell (e.g., A1)
|
||||||
// const records = await this.drizzle
|
worksheet.addImage(logoId, `I7:L17`); // Spans from A1 to C3
|
||||||
// .select({
|
}
|
||||||
// // id: trainingSurveys.id,
|
}
|
||||||
// visitDate: trainingSurveys.visitDate,
|
|
||||||
// ospName: trainingSurveys.ospName,
|
|
||||||
// productiveSector: trainingSurveys.productiveSector,
|
|
||||||
// ospAddress: trainingSurveys.ospAddress,
|
|
||||||
// ospRif: trainingSurveys.ospRif,
|
|
||||||
|
|
||||||
// siturCodeCommune: trainingSurveys.siturCodeCommune,
|
// let i = 1;
|
||||||
// communeEmail: trainingSurveys.communeEmail,
|
// while (i <= 3) {
|
||||||
// communeRif: trainingSurveys.communeRif,
|
// const element = record[`photo${i}`];
|
||||||
// communeSpokespersonName: trainingSurveys.communeSpokespersonName,
|
// if (element) {
|
||||||
// communeSpokespersonPhone: trainingSurveys.communeSpokespersonPhone,
|
// const image = element.slice(17);
|
||||||
|
// const extension: extensionType = image.split('.')[1];
|
||||||
|
|
||||||
// siturCodeCommunalCouncil: trainingSurveys.siturCodeCommunalCouncil,
|
// // Validar que sea una imagen png, gif o jpeg
|
||||||
// communalCouncilRif: trainingSurveys.communalCouncilRif,
|
// if (extension === 'png' || extension === 'gif' || extension === 'jpeg') {
|
||||||
// communalCouncilSpokespersonName:
|
// // Ruta de la imagen
|
||||||
// trainingSurveys.communalCouncilSpokespersonName,
|
// const imagePath = path.join(
|
||||||
// communalCouncilSpokespersonPhone:
|
// __dirname,
|
||||||
// trainingSurveys.communalCouncilSpokespersonPhone,
|
// '../../../',
|
||||||
|
// `uploads/training/${image}`,
|
||||||
|
// );
|
||||||
|
|
||||||
// ospType: trainingSurveys.ospType,
|
// // Add an image to the workbook from a file buffer
|
||||||
// productiveActivity: trainingSurveys.productiveActivity, // Sector Productivo
|
// const logoId = workbook.addImage({
|
||||||
// companyConstitutionYear: trainingSurveys.companyConstitutionYear,
|
// filename: imagePath,
|
||||||
// infrastructureMt2: trainingSurveys.infrastructureMt2,
|
// extension: extension,
|
||||||
|
// });
|
||||||
|
|
||||||
// hasTransport: trainingSurveys.hasTransport,
|
// // Anchor the image to a specific cell (e.g., A1)
|
||||||
// structureType: trainingSurveys.structureType,
|
// worksheet.addImage(logoId, `I7:L17`); // Spans from A1 to C3
|
||||||
// isOpenSpace: trainingSurveys.isOpenSpace,
|
// i = 4;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// i++;
|
||||||
|
// }
|
||||||
|
|
||||||
// ospResponsibleFullname: trainingSurveys.ospResponsibleFullname,
|
// Listas (Equipos, Materia Prima, Productos)
|
||||||
// ospResponsibleCedula: trainingSurveys.ospResponsibleCedula,
|
const equipmentList = Array.isArray(record.equipmentList)
|
||||||
// ospResponsiblePhone: trainingSurveys.ospResponsiblePhone,
|
? record.equipmentList
|
||||||
|
: [];
|
||||||
|
const productionList = Array.isArray(record.productionList)
|
||||||
|
? record.productionList
|
||||||
|
: [];
|
||||||
|
const productList = Array.isArray(record.productList)
|
||||||
|
? record.productList
|
||||||
|
: [];
|
||||||
|
|
||||||
// productList: trainingSurveys.productList,
|
// Colocar listas empezando en la fila 28
|
||||||
// equipmentList: trainingSurveys.equipmentList,
|
equipmentList.forEach((item: any, i: number) => {
|
||||||
// productionList: trainingSurveys.productionList,
|
const row = 28 + i;
|
||||||
|
worksheet.getCell(`A${row}`).value = item.machine;
|
||||||
|
worksheet.getCell(`C${row}`).value = item.quantity;
|
||||||
|
});
|
||||||
|
|
||||||
// // photo1: trainingSurveys.photo1
|
productionList.forEach((item: any, i: number) => {
|
||||||
// })
|
const row = 28 + i;
|
||||||
// .from(trainingSurveys)
|
worksheet.getCell(`E${row}`).value = item.rawMaterial;
|
||||||
// .where(eq(trainingSurveys.id, id));
|
worksheet.getCell(`G${row}`).value = item.quantity;
|
||||||
// // .leftJoin(states, eq(trainingSurveys.state, states.id))
|
});
|
||||||
// // .leftJoin(municipalities,eq(trainingSurveys.municipality, municipalities.id))
|
|
||||||
// // .leftJoin(parishes, eq(trainingSurveys.parish, parishes.id))
|
|
||||||
|
|
||||||
// let equipmentList: any[] = Array.isArray(records[0].equipmentList)
|
productList.forEach((item: any, i: number) => {
|
||||||
// ? records[0].equipmentList
|
const row = 28 + i;
|
||||||
// : [];
|
worksheet.getCell(`I${row}`).value = item.productName;
|
||||||
// let productList: any[] = Array.isArray(records[0].productList)
|
worksheet.getCell(`J${row}`).value = item.dailyCount;
|
||||||
// ? records[0].productList
|
worksheet.getCell(`K${row}`).value = item.weeklyCount;
|
||||||
// : [];
|
worksheet.getCell(`L${row}`).value = item.monthlyCount;
|
||||||
// let productionList: any[] = Array.isArray(records[0].productionList)
|
});
|
||||||
// ? records[0].productionList
|
|
||||||
// : [];
|
|
||||||
|
|
||||||
// console.log('equipmentList', equipmentList);
|
return await workbook.xlsx.writeBuffer();
|
||||||
// console.log('productList', productList);
|
}
|
||||||
// console.log('productionList', productionList);
|
|
||||||
|
|
||||||
// let equipmentListArray: any[] = [];
|
|
||||||
// let productListArray: any[] = [];
|
|
||||||
// let productionListArray: any[] = [];
|
|
||||||
|
|
||||||
// const equipmentListCount = equipmentList.length;
|
|
||||||
// for (let i = 0; i < equipmentListCount; i++) {
|
|
||||||
// equipmentListArray.push([
|
|
||||||
// equipmentList[i].machine,
|
|
||||||
// '',
|
|
||||||
// equipmentList[i].quantity,
|
|
||||||
// ]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const productListCount = productList.length;
|
|
||||||
// for (let i = 0; i < productListCount; i++) {
|
|
||||||
// productListArray.push([
|
|
||||||
// productList[i].productName,
|
|
||||||
// productList[i].dailyCount,
|
|
||||||
// productList[i].weeklyCount,
|
|
||||||
// productList[i].monthlyCount,
|
|
||||||
// ]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const productionListCount = productionList.length;
|
|
||||||
// for (let i = 0; i < productionListCount; i++) {
|
|
||||||
// productionListArray.push([
|
|
||||||
// productionList[i].rawMaterial,
|
|
||||||
// '',
|
|
||||||
// productionList[i].quantity,
|
|
||||||
// ]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Ruta de la plantilla
|
|
||||||
// const templatePath = path.join(
|
|
||||||
// __dirname,
|
|
||||||
// 'export_template',
|
|
||||||
// 'excel.osp.xlsx',
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // Cargar la plantilla
|
|
||||||
// const book = await XlsxPopulate.fromFileAsync(templatePath);
|
|
||||||
|
|
||||||
// const isoString = records[0].visitDate;
|
|
||||||
// const dateObj = new Date(isoString);
|
|
||||||
// const fechaFormateada = dateObj.toLocaleDateString('es-ES');
|
|
||||||
// const horaFormateada = dateObj.toLocaleTimeString('es-ES', {
|
|
||||||
// hour: '2-digit',
|
|
||||||
// minute: '2-digit',
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Llenar los datos
|
|
||||||
// book.sheet(0).cell('A6').value(records[0].productiveSector);
|
|
||||||
// book.sheet(0).cell('D6').value(records[0].ospName);
|
|
||||||
// book.sheet(0).cell('L5').value(fechaFormateada);
|
|
||||||
// book.sheet(0).cell('L6').value(horaFormateada);
|
|
||||||
// book.sheet(0).cell('B10').value(records[0].ospAddress);
|
|
||||||
// book.sheet(0).cell('C11').value(records[0].communeEmail);
|
|
||||||
// book.sheet(0).cell('C12').value(records[0].communeSpokespersonName);
|
|
||||||
// book.sheet(0).cell('G11').value(records[0].communeRif);
|
|
||||||
// book.sheet(0).cell('G12').value(records[0].communeSpokespersonPhone);
|
|
||||||
// book.sheet(0).cell('C13').value(records[0].siturCodeCommune);
|
|
||||||
// book.sheet(0).cell('G13').value(records[0].siturCodeCommunalCouncil);
|
|
||||||
// book.sheet(0).cell('G14').value(records[0].communalCouncilRif);
|
|
||||||
// book.sheet(0).cell('C15').value(records[0].communalCouncilSpokespersonName);
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .cell('G15')
|
|
||||||
// .value(records[0].communalCouncilSpokespersonPhone);
|
|
||||||
// book.sheet(0).cell('C16').value(records[0].ospType);
|
|
||||||
// book.sheet(0).cell('C17').value(records[0].ospName);
|
|
||||||
// book.sheet(0).cell('C18').value(records[0].productiveActivity);
|
|
||||||
// book.sheet(0).cell('C19').value('Proveedores');
|
|
||||||
// book.sheet(0).cell('C20').value(records[0].companyConstitutionYear);
|
|
||||||
// book.sheet(0).cell('C21').value(records[0].infrastructureMt2);
|
|
||||||
// book.sheet(0).cell('G17').value(records[0].ospRif);
|
|
||||||
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .cell(records[0].hasTransport === true ? 'J19' : 'L19')
|
|
||||||
// .value('X');
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .cell(records[0].structureType === 'CASA' ? 'J20' : 'L20')
|
|
||||||
// .value('X');
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .cell(records[0].isOpenSpace === true ? 'J21' : 'L21')
|
|
||||||
// .value('X');
|
|
||||||
|
|
||||||
// book.sheet(0).cell('A24').value(records[0].ospResponsibleFullname);
|
|
||||||
// book.sheet(0).cell('C24').value(records[0].ospResponsibleCedula);
|
|
||||||
// book.sheet(0).cell('E24').value(records[0].ospResponsiblePhone);
|
|
||||||
|
|
||||||
// book.sheet(0).cell('J24').value('N Femenino');
|
|
||||||
// book.sheet(0).cell('L24').value('N Masculino');
|
|
||||||
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .range(`A28:C${equipmentListCount + 28}`)
|
|
||||||
// .value(equipmentListArray);
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .range(`E28:G${productionListCount + 28}`)
|
|
||||||
// .value(productionListArray);
|
|
||||||
// book
|
|
||||||
// .sheet(0)
|
|
||||||
// .range(`I28:L${productListCount + 28}`)
|
|
||||||
// .value(productListArray);
|
|
||||||
|
|
||||||
// return book.outputAsync();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,5 +2,4 @@ AUTH_URL = http://localhost:3000
|
|||||||
AUTH_SECRET=wWgIwkHIGr28ydIUPsgVGNUNxXQ+brg1XXtA8PfjJjAEPJLDP2UTghWL8aE=
|
AUTH_SECRET=wWgIwkHIGr28ydIUPsgVGNUNxXQ+brg1XXtA8PfjJjAEPJLDP2UTghWL8aE=
|
||||||
API_URL=http://localhost:8000
|
API_URL=http://localhost:8000
|
||||||
NEXT_PUBLIC_API_URL=http://localhost:8000
|
NEXT_PUBLIC_API_URL=http://localhost:8000
|
||||||
NODE_ENV='development' #development | production
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export default function EditTrainingPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
// <PageContainer scrollable>
|
||||||
<div className="p-6 space-y-6">
|
<div className="p-6 space-y-6">
|
||||||
<CreateTrainingForm
|
<CreateTrainingForm
|
||||||
defaultValues={training}
|
defaultValues={training}
|
||||||
@@ -28,5 +29,6 @@ export default function EditTrainingPage() {
|
|||||||
onCancel={() => router.back()}
|
onCancel={() => router.back()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
// </PageContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// import PageContainer from '@/components/layout/page-container';
|
import PageContainer from '@/components/layout/page-container';
|
||||||
import { TrainingHeader } from '@/feactures/training/components/training-header';
|
import { TrainingHeader } from '@/feactures/training/components/training-header';
|
||||||
import TrainingList from '@/feactures/training/components/training-list';
|
import TrainingList from '@/feactures/training/components/training-list';
|
||||||
import TrainingTableAction from '@/feactures/training/components/training-tables/training-table-action';
|
import TrainingTableAction from '@/feactures/training/components/training-tables/training-table-action';
|
||||||
@@ -24,7 +24,8 @@ export default async function Page({ searchParams }: PageProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
// <PageContainer>
|
// <PageContainer>
|
||||||
<div className="flex flex-1 flex-col space-y-6 p-6">
|
// <div className="flex flex-1 flex-col space-y-6">
|
||||||
|
< div className="p-6 space-y-6" >
|
||||||
<TrainingHeader />
|
<TrainingHeader />
|
||||||
<TrainingTableAction />
|
<TrainingTableAction />
|
||||||
<TrainingList
|
<TrainingList
|
||||||
@@ -33,7 +34,7 @@ export default async function Page({ searchParams }: PageProps) {
|
|||||||
initialLimit={limit || 10}
|
initialLimit={limit || 10}
|
||||||
apiUrl={env.API_URL}
|
apiUrl={env.API_URL}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div >
|
||||||
// </PageContainer>
|
// </PageContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { auth } from '@/lib/auth';
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
export default async function Dashboard() {
|
export default async function Dashboard() {
|
||||||
|
console.log('La sesion es llamada');
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|
||||||
if (!session?.user) {
|
if (!session?.user) {
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const RootLayout = async ({
|
|||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}>) => {
|
}>) => {
|
||||||
|
console.log('La sesion es llamada');
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
return (
|
return (
|
||||||
<html lang="en" suppressHydrationWarning>
|
<html lang="en" suppressHydrationWarning>
|
||||||
|
|||||||
@@ -1,195 +1,196 @@
|
|||||||
export const COUNTRY_OPTIONS = [
|
export const COUNTRY_OPTIONS = [
|
||||||
'Afganistán',
|
'Afganistán',
|
||||||
'Albania',
|
'Albania',
|
||||||
'Alemania',
|
'Alemania',
|
||||||
'Andorra',
|
'Andorra',
|
||||||
'Angola',
|
'Angola',
|
||||||
'Antigua y Barbuda',
|
'Antigua y Barbuda',
|
||||||
'Arabia Saudita',
|
'Arabia Saudita',
|
||||||
'Argelia',
|
'Argelia',
|
||||||
'Argentina',
|
'Argentina',
|
||||||
'Armenia',
|
'Armenia',
|
||||||
'Australia',
|
'Australia',
|
||||||
'Austria',
|
'Austria',
|
||||||
'Azerbaiyán',
|
'Azerbaiyán',
|
||||||
'Bahamas',
|
'Bahamas',
|
||||||
'Bangladés',
|
'Bangladés',
|
||||||
'Barbados',
|
'Barbados',
|
||||||
'Baréin',
|
'Baréin',
|
||||||
'Bélgica',
|
'Bélgica',
|
||||||
'Belice',
|
'Belice',
|
||||||
'Benín',
|
'Benín',
|
||||||
'Bielorrusia',
|
'Bielorrusia',
|
||||||
'Birmania',
|
'Birmania',
|
||||||
'Bolivia',
|
'Bolivia',
|
||||||
'Bosnia y Herzegovina',
|
'Bosnia y Herzegovina',
|
||||||
'Botsuana',
|
'Botsuana',
|
||||||
'Brasil',
|
'Brasil',
|
||||||
'Brunéi',
|
'Brunéi',
|
||||||
'Bulgaria',
|
'Bulgaria',
|
||||||
'Burkina Faso',
|
'Burkina Faso',
|
||||||
'Burundi',
|
'Burundi',
|
||||||
'Bután',
|
'Bután',
|
||||||
'Cabo Verde',
|
'Cabo Verde',
|
||||||
'Camboya',
|
'Camboya',
|
||||||
'Camerún',
|
'Camerún',
|
||||||
'Canadá',
|
'Canadá',
|
||||||
'Catar',
|
'Catar',
|
||||||
'Chad',
|
'Chad',
|
||||||
'Chile',
|
'Chile',
|
||||||
'China',
|
'China',
|
||||||
'Chipre',
|
'Chipre',
|
||||||
'Ciudad del Vaticano',
|
'Ciudad del Vaticano',
|
||||||
'Colombia',
|
'Colombia',
|
||||||
'Comoras',
|
'Comoras',
|
||||||
'Corea del Norte',
|
'Corea del Norte',
|
||||||
'Corea del Sur',
|
'Corea del Sur',
|
||||||
'Costa de Marfil',
|
'Costa de Marfil',
|
||||||
'Costa Rica',
|
'Costa Rica',
|
||||||
'Croacia',
|
'Croacia',
|
||||||
'Cuba',
|
'Cuba',
|
||||||
'Dinamarca',
|
'Dinamarca',
|
||||||
'Dominica',
|
'Dominica',
|
||||||
'Ecuador',
|
'Ecuador',
|
||||||
'Egipto',
|
'Egipto',
|
||||||
'El Salvador',
|
'El Salvador',
|
||||||
'Emiratos Árabes Unidos',
|
'Emiratos Árabes Unidos',
|
||||||
'Eritrea',
|
'Eritrea',
|
||||||
'Eslovaquia',
|
'Eslovaquia',
|
||||||
'Eslovenia',
|
'Eslovenia',
|
||||||
'España',
|
'España',
|
||||||
'Estados Unidos',
|
'Estados Unidos',
|
||||||
'Estonia',
|
'Estonia',
|
||||||
'Etiopía',
|
'Etiopía',
|
||||||
'Filipinas',
|
'Filipinas',
|
||||||
'Finlandia',
|
'Finlandia',
|
||||||
'Fiyi',
|
'Fiyi',
|
||||||
'Francia',
|
'Francia',
|
||||||
'Gabón',
|
'Gabón',
|
||||||
'Gambia',
|
'Gambia',
|
||||||
'Georgia',
|
'Georgia',
|
||||||
'Ghana',
|
'Ghana',
|
||||||
'Granada',
|
'Granada',
|
||||||
'Grecia',
|
'Grecia',
|
||||||
'Guatemala',
|
'Guatemala',
|
||||||
'Guyana',
|
'Guyana',
|
||||||
'Guinea',
|
'Guinea',
|
||||||
'Guinea Ecuatorial',
|
'Guinea Ecuatorial',
|
||||||
'Guinea-Bisáu',
|
'Guinea-Bisáu',
|
||||||
'Haití',
|
'Haití',
|
||||||
'Honduras',
|
'Honduras',
|
||||||
'Hungría',
|
'Hungría',
|
||||||
'India',
|
'India',
|
||||||
'Indonesia',
|
'Indonesia',
|
||||||
'Irak',
|
'Irak',
|
||||||
'Irán',
|
'Irán',
|
||||||
'Irlanda',
|
'Irlanda',
|
||||||
'Islandia',
|
'Islandia',
|
||||||
'Islas Marshall',
|
'Islas Marshall',
|
||||||
'Islas Salomón',
|
'Islas Salomón',
|
||||||
'Israel',
|
'Israel',
|
||||||
'Italia',
|
'Italia',
|
||||||
'Jamaica',
|
'Jamaica',
|
||||||
'Japón',
|
'Japón',
|
||||||
'Jordania',
|
'Jordania',
|
||||||
'Kazajistán',
|
'Kazajistán',
|
||||||
'Kenia',
|
'Kenia',
|
||||||
'Kirguistán',
|
'Kirguistán',
|
||||||
'Kiribati',
|
'Kiribati',
|
||||||
'Kuwait',
|
'Kuwait',
|
||||||
'Laos',
|
'Laos',
|
||||||
'Lesoto',
|
'Lesoto',
|
||||||
'Letonia',
|
'Letonia',
|
||||||
'Líbano',
|
'Líbano',
|
||||||
'Liberia',
|
'Liberia',
|
||||||
'Libia',
|
'Libia',
|
||||||
'Liechtenstein',
|
'Liechtenstein',
|
||||||
'Lituania',
|
'Lituania',
|
||||||
'Luxemburgo',
|
'Luxemburgo',
|
||||||
'Madagascar',
|
'Madagascar',
|
||||||
'Malasia',
|
'Malasia',
|
||||||
'Malaui',
|
'Malaui',
|
||||||
'Maldivas',
|
'Maldivas',
|
||||||
'Malí',
|
'Malí',
|
||||||
'Malta',
|
'Malta',
|
||||||
'Marruecos',
|
'Marruecos',
|
||||||
'Mauricio',
|
'Mauricio',
|
||||||
'Mauritania',
|
'Mauritania',
|
||||||
'México',
|
'México',
|
||||||
'Micronesia',
|
'Micronesia',
|
||||||
'Moldavia',
|
'Moldavia',
|
||||||
'Mónaco',
|
'Mónaco',
|
||||||
'Mongolia',
|
'Mongolia',
|
||||||
'Montenegro',
|
'Montenegro',
|
||||||
'Mozambique',
|
'Mozambique',
|
||||||
'Namibia',
|
'Namibia',
|
||||||
'Nauru',
|
'Nauru',
|
||||||
'Nepal',
|
'Nepal',
|
||||||
'Nicaragua',
|
'Nicaragua',
|
||||||
'Níger',
|
'Níger',
|
||||||
'Nigeria',
|
'Nigeria',
|
||||||
'Noruega',
|
'Noruega',
|
||||||
'Nueva Zelanda',
|
'Nueva Zelanda',
|
||||||
'Omán',
|
'Omán',
|
||||||
'Países Bajos',
|
'Países Bajos',
|
||||||
'Pakistán',
|
'Pakistán',
|
||||||
'Palaos',
|
'Palaos',
|
||||||
'Panamá',
|
'Panamá',
|
||||||
'Papúa Nueva Guinea',
|
'Papúa Nueva Guinea',
|
||||||
'Paraguay',
|
'Paraguay',
|
||||||
'Perú',
|
'Perú',
|
||||||
'Polonia',
|
'Polonia',
|
||||||
'Portugal',
|
'Portugal',
|
||||||
'Reino Unido',
|
'Reino Unido',
|
||||||
'República Centroafricana',
|
'República Centroafricana',
|
||||||
'República Checa',
|
'República Checa',
|
||||||
'República de Macedonia',
|
'República de Macedonia',
|
||||||
'República del Congo',
|
'República del Congo',
|
||||||
'República Democrática del Congo',
|
'República Democrática del Congo',
|
||||||
'República Dominicana',
|
'República Dominicana',
|
||||||
'República Sudafricana',
|
'República Sudafricana',
|
||||||
'Ruanda',
|
'Ruanda',
|
||||||
'Rumanía',
|
'Rumanía',
|
||||||
'Rusia',
|
'Rusia',
|
||||||
'Samoa',
|
'Samoa',
|
||||||
'San Cristóbal y Nieves',
|
'San Cristóbal y Nieves',
|
||||||
'San Marino',
|
'San Marino',
|
||||||
'San Vicente y las Granadinas',
|
'San Vicente y las Granadinas',
|
||||||
'Santa Lucía',
|
'Santa Lucía',
|
||||||
'Santo Tomé y Príncipe',
|
'Santo Tomé y Príncipe',
|
||||||
'Senegal',
|
'Senegal',
|
||||||
'Serbia',
|
'Serbia',
|
||||||
'Seychelles',
|
'Seychelles',
|
||||||
'Sierra Leona',
|
'Sierra Leona',
|
||||||
'Singapur',
|
'Singapur',
|
||||||
'Siria',
|
'Siria',
|
||||||
'Somalia',
|
'Somalia',
|
||||||
'Sri Lanka',
|
'Sri Lanka',
|
||||||
'Suazilandia',
|
'Suazilandia',
|
||||||
'Sudán',
|
'Sudán',
|
||||||
'Sudán del Sur',
|
'Sudán del Sur',
|
||||||
'Suecia',
|
'Suecia',
|
||||||
'Suiza',
|
'Suiza',
|
||||||
'Surinam',
|
'Surinam',
|
||||||
'Tailandia',
|
'Tailandia',
|
||||||
'Tanzania',
|
'Tanzania',
|
||||||
'Tayikistán',
|
'Tayikistán',
|
||||||
'Timor Oriental',
|
'Timor Oriental',
|
||||||
'Togo',
|
'Togo',
|
||||||
'Tonga',
|
'Tonga',
|
||||||
'Trinidad y Tobago',
|
'Trinidad y Tobago',
|
||||||
'Túnez',
|
'Túnez',
|
||||||
'Turkmenistán',
|
'Turkmenistán',
|
||||||
'Turquía',
|
'Turquía',
|
||||||
'Tuvalu',
|
'Tuvalu',
|
||||||
'Ucrania',
|
'Ucrania',
|
||||||
'Uganda',
|
'Uganda',
|
||||||
'Uruguay',
|
'Uruguay',
|
||||||
'Uzbekistán',
|
'Uzbekistán',
|
||||||
'Vanuatu',
|
'Vanuatu',
|
||||||
'Vietnam',
|
'Venezuela',
|
||||||
'Yemen',
|
'Vietnam',
|
||||||
'Yibuti',
|
'Yemen',
|
||||||
'Zambia',
|
'Yibuti',
|
||||||
'Zimbabue',
|
'Zambia',
|
||||||
];
|
'Zimbabue'
|
||||||
|
];
|
||||||
@@ -34,7 +34,7 @@ export const AdministrationItems: NavItem[] = [
|
|||||||
url: '/dashboard/administracion/usuario',
|
url: '/dashboard/administracion/usuario',
|
||||||
icon: 'userPen',
|
icon: 'userPen',
|
||||||
shortcut: ['m', 'm'],
|
shortcut: ['m', 'm'],
|
||||||
role: ['admin', 'superadmin'],
|
role: ['admin', 'superadmin', 'autoridad'],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Encuestas',
|
title: 'Encuestas',
|
||||||
@@ -60,7 +60,7 @@ export const StatisticsItems: NavItem[] = [
|
|||||||
url: '#', // Placeholder as there is no direct link for the parent
|
url: '#', // Placeholder as there is no direct link for the parent
|
||||||
icon: 'chartColumn',
|
icon: 'chartColumn',
|
||||||
isActive: true,
|
isActive: true,
|
||||||
role: ['admin', 'superadmin', 'autoridad'],
|
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||||
|
|
||||||
items: [
|
items: [
|
||||||
// {
|
// {
|
||||||
@@ -82,7 +82,7 @@ export const StatisticsItems: NavItem[] = [
|
|||||||
shortcut: ['s', 's'],
|
shortcut: ['s', 's'],
|
||||||
url: '/dashboard/estadisticas/socioproductiva',
|
url: '/dashboard/estadisticas/socioproductiva',
|
||||||
icon: 'blocks',
|
icon: 'blocks',
|
||||||
role: ['admin', 'superadmin', 'autoridad'],
|
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
'use server';
|
'use server';
|
||||||
import { safeFetchApi } from '@/lib';
|
import { safeFetchApi } from '@/lib/fetch.api';
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
import { loginResponseSchema, UserFormValue } from '../schemas/login';
|
import { loginResponseSchema, UserFormValue } from '../schemas/login';
|
||||||
|
|
||||||
type LoginActionSuccess = {
|
type LoginActionSuccess = {
|
||||||
@@ -18,7 +17,7 @@ type LoginActionSuccess = {
|
|||||||
refresh_token: string;
|
refresh_token: string;
|
||||||
refresh_expire_in: number;
|
refresh_expire_in: number;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type LoginActionError = {
|
type LoginActionError = {
|
||||||
type: 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR'; // **Asegúrate de que el tipo de `type` sea este aquí**
|
type: 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR'; // **Asegúrate de que el tipo de `type` sea este aquí**
|
||||||
@@ -29,7 +28,7 @@ type LoginActionError = {
|
|||||||
// Si SignInAction también puede devolver null, asegúralo en su tipo de retorno
|
// Si SignInAction también puede devolver null, asegúralo en su tipo de retorno
|
||||||
type LoginActionResult = LoginActionSuccess | LoginActionError | null;
|
type LoginActionResult = LoginActionSuccess | LoginActionError | null;
|
||||||
|
|
||||||
export const SignInAction = async (payload: UserFormValue) => {
|
export const SignInAction = async (payload: UserFormValue): Promise<LoginActionResult> => {
|
||||||
const [error, data] = await safeFetchApi(
|
const [error, data] = await safeFetchApi(
|
||||||
loginResponseSchema,
|
loginResponseSchema,
|
||||||
'/auth/sign-in',
|
'/auth/sign-in',
|
||||||
@@ -37,22 +36,12 @@ export const SignInAction = async (payload: UserFormValue) => {
|
|||||||
payload,
|
payload,
|
||||||
);
|
);
|
||||||
if (error) {
|
if (error) {
|
||||||
return error;
|
return {
|
||||||
|
type: error.type as 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR',
|
||||||
|
message: error.message,
|
||||||
|
details: error.details
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
// 2. GUARDAR REFRESH TOKEN EN COOKIE (La clave del cambio)
|
|
||||||
|
|
||||||
(await cookies()).set(
|
|
||||||
'refresh_token',
|
|
||||||
String(data?.tokens?.refresh_token),
|
|
||||||
{
|
|
||||||
httpOnly: true, // JavaScript no puede leerla
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
sameSite: 'lax',
|
|
||||||
path: '/',
|
|
||||||
maxAge: 7 * 24 * 60 * 60, // Ej: 7 días (debe coincidir con tu backend)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
// import { safeFetchApi } from '@/lib';
|
|
||||||
import { refreshApi } from '@/lib/refreshApi'; // Importa la nueva instancia
|
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
import { logoutResponseSchema } from '../schemas/logout';
|
|
||||||
|
|
||||||
export const logoutAction = async (user_id: string) => {
|
|
||||||
try {
|
|
||||||
const response = await refreshApi.post('/auth/sign-out', { user_id });
|
|
||||||
|
|
||||||
const parsed = logoutResponseSchema.safeParse(response.data);
|
|
||||||
|
|
||||||
if (!parsed.success) {
|
|
||||||
console.error('Error de validación en la respuesta de refresh token:', {
|
|
||||||
errors: parsed.error.errors,
|
|
||||||
receivedData: response.data,
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed.data;
|
|
||||||
} catch (error: any) { // Captura el error para acceso a error.response
|
|
||||||
console.error('Error al cerrar sesion:', error.response?.data || error.message);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// const payload = { user_id };
|
|
||||||
// const [error, data] = await safeFetchApi(
|
|
||||||
// logoutResponseSchema,
|
|
||||||
// '/auth/sign-out',
|
|
||||||
// 'POST',
|
|
||||||
// payload,
|
|
||||||
// );
|
|
||||||
|
|
||||||
// if (error) {
|
|
||||||
// console.error('Error:', error);
|
|
||||||
// // Devuelve un objeto con la propiedad 'type' para que el callback de NextAuth lo reconozca como un error
|
|
||||||
// return {
|
|
||||||
// type: 'API_ERROR',
|
|
||||||
// message: error.message,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
(await cookies()).delete('refresh_token');
|
|
||||||
};
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// auth/actions/refresh-token-action.ts
|
||||||
'use server';
|
'use server';
|
||||||
import { refreshApi } from '@/lib/refreshApi'; // Importa la nueva instancia
|
import { refreshApi } from '@/lib/refreshApi'; // Importa la nueva instancia
|
||||||
import {
|
import {
|
||||||
@@ -9,7 +10,7 @@ export const resfreshTokenAction = async (refreshToken: RefreshTokenValue) => {
|
|||||||
try {
|
try {
|
||||||
const response = await refreshApi.patch('/auth/refresh', refreshToken);
|
const response = await refreshApi.patch('/auth/refresh', refreshToken);
|
||||||
|
|
||||||
const parsed = RefreshTokenResponseSchema.safeParse(response.data);
|
const parsed = RefreshTokenResponseSchema.safeParse(response.data);
|
||||||
|
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
console.error('Error de validación en la respuesta de refresh token:', {
|
console.error('Error de validación en la respuesta de refresh token:', {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
import z from 'zod';
|
|
||||||
|
|
||||||
export const logoutResponseSchema = z.object({
|
|
||||||
message: z.string(),
|
|
||||||
});
|
|
||||||
@@ -4,19 +4,13 @@ import { tokensSchema } from './login';
|
|||||||
|
|
||||||
// Esquema para el refresh token
|
// Esquema para el refresh token
|
||||||
export const refreshTokenSchema = z.object({
|
export const refreshTokenSchema = z.object({
|
||||||
refreshToken: z.string(),
|
user_id: z.number(),
|
||||||
|
token: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type RefreshTokenValue = z.infer<typeof refreshTokenSchema>;
|
export type RefreshTokenValue = z.infer<typeof refreshTokenSchema>;
|
||||||
|
|
||||||
// Esquema final para la respuesta del backend
|
// Esquema final para la respuesta del backend
|
||||||
// export const RefreshTokenResponseSchema = z.object({
|
export const RefreshTokenResponseSchema = z.object({
|
||||||
// // tokens: tokensSchema,
|
tokens: tokensSchema,
|
||||||
// access_token: z.string(),
|
});
|
||||||
// access_expire_in: z.number(),
|
|
||||||
// refresh_token: z.string(),
|
|
||||||
// refresh_expire_in: z.number()
|
|
||||||
// });
|
|
||||||
|
|
||||||
export const RefreshTokenResponseSchema = tokensSchema
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
import { Button } from '@repo/shadcn/button';
|
import { Button } from '@repo/shadcn/button';
|
||||||
import { Heading } from '@repo/shadcn/heading';
|
import { Heading } from '@repo/shadcn/heading';
|
||||||
import { Plus } from 'lucide-react';
|
import { Plus } from 'lucide-react';
|
||||||
import { useSession } from 'next-auth/react';
|
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
|
|
||||||
export function SurveysHeader() {
|
export function SurveysHeader() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data: session } = useSession();
|
|
||||||
const role = session?.user.role[0]?.rol;
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex items-start justify-between">
|
<div className="flex items-start justify-between">
|
||||||
@@ -16,18 +14,11 @@ export function SurveysHeader() {
|
|||||||
title="Administración de Encuestas"
|
title="Administración de Encuestas"
|
||||||
description="Gestiona las encuestas disponibles en la plataforma"
|
description="Gestiona las encuestas disponibles en la plataforma"
|
||||||
/>
|
/>
|
||||||
{['superadmin', 'admin'].includes(role ?? '') && (
|
<Button onClick={() => router.push(`/dashboard/administracion/encuestas/crear`)} size="sm">
|
||||||
<Button
|
<Plus className="h-4 w-4"/><span className='hidden sm:inline'>Agregar Encuesta</span>
|
||||||
onClick={() =>
|
</Button>
|
||||||
router.push(`/dashboard/administracion/encuestas/crear`)
|
|
||||||
}
|
|
||||||
size="sm"
|
|
||||||
>
|
|
||||||
<Plus className="h-4 w-4" />
|
|
||||||
<span className="hidden sm:inline">Agregar Encuesta</span>
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
import { AlertModal } from '@/components/modal/alert-modal';
|
import { AlertModal } from '@/components/modal/alert-modal';
|
||||||
import { useDeleteSurvey } from '@/feactures/surveys/hooks/use-mutation-surveys';
|
|
||||||
import { SurveyTable } from '@/feactures/surveys/schemas/survey';
|
|
||||||
import { Button } from '@repo/shadcn/button';
|
import { Button } from '@repo/shadcn/button';
|
||||||
import {
|
import {
|
||||||
Tooltip,
|
Tooltip,
|
||||||
@@ -10,9 +10,9 @@ import {
|
|||||||
TooltipTrigger,
|
TooltipTrigger,
|
||||||
} from '@repo/shadcn/tooltip';
|
} from '@repo/shadcn/tooltip';
|
||||||
import { Edit, Trash } from 'lucide-react';
|
import { Edit, Trash } from 'lucide-react';
|
||||||
import { useSession } from 'next-auth/react';
|
import { SurveyTable } from '@/feactures/surveys/schemas/survey';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useDeleteSurvey } from '@/feactures/surveys/hooks/use-mutation-surveys';
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
interface CellActionProps {
|
interface CellActionProps {
|
||||||
data: SurveyTable;
|
data: SurveyTable;
|
||||||
@@ -23,7 +23,6 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
|||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const { mutate: deleteSurvey } = useDeleteSurvey();
|
const { mutate: deleteSurvey } = useDeleteSurvey();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data: session } = useSession();
|
|
||||||
|
|
||||||
const onConfirm = async () => {
|
const onConfirm = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -37,8 +36,6 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const role = session?.user.role[0]?.rol;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AlertModal
|
<AlertModal
|
||||||
@@ -50,48 +47,41 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
|||||||
description="Esta acción no se puede deshacer."
|
description="Esta acción no se puede deshacer."
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="flex gap-1">
|
|
||||||
{['superadmin', 'admin'].includes(role ?? '') && (
|
|
||||||
<>
|
|
||||||
<TooltipProvider>
|
|
||||||
<Tooltip>
|
|
||||||
<TooltipTrigger asChild>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="icon"
|
|
||||||
onClick={() =>
|
|
||||||
router.push(
|
|
||||||
`/dashboard/administracion/encuestas/editar/${data.id!}`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Edit className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</TooltipTrigger>
|
|
||||||
<TooltipContent>
|
|
||||||
<p>Editar</p>
|
|
||||||
</TooltipContent>
|
|
||||||
</Tooltip>
|
|
||||||
</TooltipProvider>
|
|
||||||
|
|
||||||
<TooltipProvider>
|
<div className="flex gap-1">
|
||||||
<Tooltip>
|
<TooltipProvider>
|
||||||
<TooltipTrigger asChild>
|
<Tooltip>
|
||||||
<Button
|
<TooltipTrigger asChild>
|
||||||
variant="outline"
|
<Button
|
||||||
size="icon"
|
variant="outline"
|
||||||
onClick={() => setOpen(true)}
|
size="icon"
|
||||||
>
|
onClick={() => router.push(`/dashboard/administracion/encuestas/editar/${data.id!}`)}
|
||||||
<Trash className="h-4 w-4" />
|
>
|
||||||
</Button>
|
<Edit className="h-4 w-4" />
|
||||||
</TooltipTrigger>
|
</Button>
|
||||||
<TooltipContent>
|
</TooltipTrigger>
|
||||||
<p>Eliminar</p>
|
<TooltipContent>
|
||||||
</TooltipContent>
|
<p>Editar</p>
|
||||||
</Tooltip>
|
</TooltipContent>
|
||||||
</TooltipProvider>
|
</Tooltip>
|
||||||
</>
|
</TooltipProvider>
|
||||||
)}
|
|
||||||
|
<TooltipProvider>
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="icon"
|
||||||
|
onClick={() => setOpen(true)}
|
||||||
|
>
|
||||||
|
<Trash className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<p>Eliminar</p>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ export const createTrainingAction = async (
|
|||||||
payloadToSend = rest as any;
|
payloadToSend = rest as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// console.log(payloadToSend);
|
||||||
|
|
||||||
const [error, data] = await safeFetchApi(
|
const [error, data] = await safeFetchApi(
|
||||||
TrainingMutate,
|
TrainingMutate,
|
||||||
'/training',
|
'/training',
|
||||||
|
|||||||
@@ -20,34 +20,24 @@ import { Label } from '@repo/shadcn/label';
|
|||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2 } from 'lucide-react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||||
import { TrainingSchema } from '../schemas/training';
|
|
||||||
|
|
||||||
interface EquipmentItem {
|
|
||||||
machine: string;
|
|
||||||
quantity: string | number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function EquipmentList() {
|
export function EquipmentList() {
|
||||||
const { control, register } = useFormContext<TrainingSchema>();
|
const { control, register } = useFormContext();
|
||||||
const { fields, append, remove } = useFieldArray({
|
const { fields, append, remove } = useFieldArray({
|
||||||
control,
|
control,
|
||||||
name: 'equipmentList',
|
name: 'equipmentList',
|
||||||
});
|
});
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [newItem, setNewItem] = useState<EquipmentItem>({
|
const [newItem, setNewItem] = useState({
|
||||||
machine: '',
|
machine: '',
|
||||||
|
specifications: '',
|
||||||
quantity: '',
|
quantity: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleAdd = (e: React.MouseEvent) => {
|
const handleAdd = () => {
|
||||||
e.preventDefault();
|
if (newItem.machine && newItem.quantity) {
|
||||||
e.stopPropagation();
|
append({ ...newItem, quantity: Number(newItem.quantity) });
|
||||||
if (newItem.machine.trim()) {
|
setNewItem({ machine: '', specifications: '', quantity: '' });
|
||||||
append({
|
|
||||||
machine: newItem.machine,
|
|
||||||
quantity: newItem.quantity ? Number(newItem.quantity) : 0,
|
|
||||||
});
|
|
||||||
setNewItem({ machine: '', quantity: '' });
|
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -58,11 +48,9 @@ export function EquipmentList() {
|
|||||||
<h3 className="text-lg font-medium">Datos del Equipamiento</h3>
|
<h3 className="text-lg font-medium">Datos del Equipamiento</h3>
|
||||||
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<Button variant="outline" type="button">
|
<Button variant="outline">Agregar Maquinaria</Button>
|
||||||
Agregar Maquinaria
|
|
||||||
</Button>
|
|
||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
<DialogContent onPointerDownOutside={(e) => e.preventDefault()}>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Agregar Maquinaria/Equipo</DialogTitle>
|
<DialogTitle>Agregar Maquinaria/Equipo</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
@@ -71,9 +59,8 @@ export function EquipmentList() {
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="modal-machine">Maquinaria</Label>
|
<Label>Maquinaria</Label>
|
||||||
<Input
|
<Input
|
||||||
id="modal-machine"
|
|
||||||
value={newItem.machine}
|
value={newItem.machine}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
setNewItem({ ...newItem, machine: e.target.value })
|
setNewItem({ ...newItem, machine: e.target.value })
|
||||||
@@ -82,9 +69,18 @@ export function EquipmentList() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="modal-quantity">Cantidad</Label>
|
<Label>Especificaciones</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.specifications}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, specifications: e.target.value })
|
||||||
|
}
|
||||||
|
placeholder="Especificaciones técnicas"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Cantidad</Label>
|
||||||
<Input
|
<Input
|
||||||
id="modal-quantity"
|
|
||||||
type="number"
|
type="number"
|
||||||
value={newItem.quantity}
|
value={newItem.quantity}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
@@ -97,17 +93,12 @@ export function EquipmentList() {
|
|||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
type="button"
|
type="button"
|
||||||
onClick={(e) => {
|
onClick={() => setIsOpen(false)}
|
||||||
e.preventDefault();
|
|
||||||
setIsOpen(false);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
Cancelar
|
Cancelar
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button type="button" onClick={handleAdd}>
|
<Button onClick={handleAdd}>Guardar</Button>
|
||||||
Guardar
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
@@ -119,6 +110,7 @@ export function EquipmentList() {
|
|||||||
<TableHeader>
|
<TableHeader>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableHead>Maquinaria</TableHead>
|
<TableHead>Maquinaria</TableHead>
|
||||||
|
<TableHead>Especificaciones</TableHead>
|
||||||
<TableHead>Cantidad</TableHead>
|
<TableHead>Cantidad</TableHead>
|
||||||
<TableHead className="w-[50px]"></TableHead>
|
<TableHead className="w-[50px]"></TableHead>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
@@ -130,27 +122,31 @@ export function EquipmentList() {
|
|||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
{...register(`equipmentList.${index}.machine`)}
|
{...register(`equipmentList.${index}.machine`)}
|
||||||
defaultValue={field.machine ?? ''}
|
|
||||||
/>
|
/>
|
||||||
|
{/* @ts-ignore */}
|
||||||
{field.machine}
|
{field.machine}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
{...register(`equipmentList.${index}.quantity`)}
|
{...register(`equipmentList.${index}.specifications`)}
|
||||||
defaultValue={field.quantity ?? ''}
|
|
||||||
/>
|
/>
|
||||||
|
{/* @ts-ignore */}
|
||||||
|
{field.specifications}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
{...register(`equipmentList.${index}.quantity`)}
|
||||||
|
/>
|
||||||
|
{/* @ts-ignore */}
|
||||||
{field.quantity}
|
{field.quantity}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
type="button"
|
onClick={() => remove(index)}
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
remove(index);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<Trash2 className="h-4 w-4 text-destructive" />
|
<Trash2 className="h-4 w-4 text-destructive" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -160,7 +156,7 @@ export function EquipmentList() {
|
|||||||
{fields.length === 0 && (
|
{fields.length === 0 && (
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell
|
<TableCell
|
||||||
colSpan={3}
|
colSpan={4}
|
||||||
className="text-center text-muted-foreground"
|
className="text-center text-muted-foreground"
|
||||||
>
|
>
|
||||||
No hay equipamiento registrado
|
No hay equipamiento registrado
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,9 @@
|
|||||||
|
import { COUNTRY_OPTIONS } from '@/constants/countries';
|
||||||
|
import {
|
||||||
|
useMunicipalityQuery,
|
||||||
|
useParishQuery,
|
||||||
|
useStateQuery,
|
||||||
|
} from '@/feactures/location/hooks/use-query-location';
|
||||||
import { Button } from '@repo/shadcn/button';
|
import { Button } from '@repo/shadcn/button';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@@ -17,18 +23,36 @@ import {
|
|||||||
} from '@repo/shadcn/components/ui/table';
|
} from '@repo/shadcn/components/ui/table';
|
||||||
import { Input } from '@repo/shadcn/input';
|
import { Input } from '@repo/shadcn/input';
|
||||||
import { Label } from '@repo/shadcn/label';
|
import { Label } from '@repo/shadcn/label';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@repo/shadcn/select';
|
||||||
|
import { SelectSearchable } from '@repo/shadcn/select-searchable';
|
||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2 } from 'lucide-react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||||
import { TrainingSchema } from '../schemas/training';
|
|
||||||
|
|
||||||
const UNIT_OPTIONS = ['KG', 'TON', 'UNID', 'LT', 'MTS', 'QQ', 'HM2', 'SACOS'];
|
|
||||||
|
|
||||||
// 1. Definimos la estructura de los datos para que TypeScript no se queje
|
// 1. Definimos la estructura de los datos para que TypeScript no se queje
|
||||||
// ProductItem y ProductFormValues locales eliminados en favor de TrainingSchema
|
interface ProductItem {
|
||||||
|
productName: string;
|
||||||
|
description: string;
|
||||||
|
dailyCount: string;
|
||||||
|
weeklyCount: string;
|
||||||
|
monthlyCount: string;
|
||||||
|
// ... resto de propiedades opcionales si las necesitas tipar estrictamente
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProductFormValues {
|
||||||
|
productList: ProductItem[];
|
||||||
|
}
|
||||||
|
|
||||||
export function ProductActivityList() {
|
export function ProductActivityList() {
|
||||||
const { control, register } = useFormContext<TrainingSchema>();
|
// 2. Pasamos el tipo genérico a useFormContext
|
||||||
|
const { control, register } = useFormContext<ProductFormValues>();
|
||||||
|
|
||||||
const { fields, append, remove } = useFieldArray({
|
const { fields, append, remove } = useFieldArray({
|
||||||
control,
|
control,
|
||||||
@@ -39,25 +63,91 @@ export function ProductActivityList() {
|
|||||||
|
|
||||||
// Modal Form State
|
// Modal Form State
|
||||||
const [newItem, setNewItem] = useState<any>({
|
const [newItem, setNewItem] = useState<any>({
|
||||||
|
productName: '',
|
||||||
description: '',
|
description: '',
|
||||||
dailyCount: '',
|
dailyCount: '',
|
||||||
weeklyCount: '',
|
weeklyCount: '',
|
||||||
monthlyCount: '',
|
monthlyCount: '',
|
||||||
|
|
||||||
|
// Internal dist
|
||||||
|
internalState: undefined,
|
||||||
|
internalMunicipality: undefined,
|
||||||
|
internalParish: undefined,
|
||||||
|
internalDescription: '',
|
||||||
|
internalQuantity: '',
|
||||||
|
internalUnit: '',
|
||||||
|
|
||||||
|
// External dist
|
||||||
|
externalCountry: '',
|
||||||
|
externalState: undefined,
|
||||||
|
externalMunicipality: undefined,
|
||||||
|
externalParish: undefined,
|
||||||
|
externalCity: '',
|
||||||
|
externalDescription: '',
|
||||||
|
externalQuantity: '',
|
||||||
|
externalUnit: '',
|
||||||
|
|
||||||
|
// Workforce
|
||||||
|
womenCount: '',
|
||||||
|
menCount: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Location logic for Internal Validation
|
||||||
|
const [internalStateId, setInternalStateId] = useState(0);
|
||||||
|
const [internalMuniId, setInternalMuniId] = useState(0);
|
||||||
|
|
||||||
|
const { data: statesData } = useStateQuery();
|
||||||
|
const { data: internalMuniData } = useMunicipalityQuery(internalStateId);
|
||||||
|
const { data: internalParishData } = useParishQuery(internalMuniId);
|
||||||
|
|
||||||
|
// Location logic for External Validation
|
||||||
|
const [externalStateId, setExternalStateId] = useState(0);
|
||||||
|
const [externalMuniId, setExternalMuniId] = useState(0);
|
||||||
|
const { data: externalMuniData } = useMunicipalityQuery(externalStateId);
|
||||||
|
const { data: externalParishData } = useParishQuery(externalMuniId);
|
||||||
|
|
||||||
|
const isVenezuela = newItem.externalCountry === 'Venezuela';
|
||||||
|
|
||||||
const handleAdd = () => {
|
const handleAdd = () => {
|
||||||
if (newItem.description) {
|
if (newItem.productName) {
|
||||||
append(newItem);
|
append(newItem);
|
||||||
setNewItem({
|
setNewItem({
|
||||||
|
productName: '',
|
||||||
description: '',
|
description: '',
|
||||||
dailyCount: '',
|
dailyCount: '',
|
||||||
weeklyCount: '',
|
weeklyCount: '',
|
||||||
monthlyCount: '',
|
monthlyCount: '',
|
||||||
|
internalState: undefined,
|
||||||
|
internalMunicipality: undefined,
|
||||||
|
internalParish: undefined,
|
||||||
|
internalDescription: '',
|
||||||
|
internalQuantity: '',
|
||||||
|
internalUnit: '',
|
||||||
|
externalCountry: '',
|
||||||
|
externalState: undefined,
|
||||||
|
externalMunicipality: undefined,
|
||||||
|
externalParish: undefined,
|
||||||
|
externalCity: '',
|
||||||
|
externalDescription: '',
|
||||||
|
externalQuantity: '',
|
||||||
|
externalUnit: '',
|
||||||
|
womenCount: '',
|
||||||
|
menCount: '',
|
||||||
});
|
});
|
||||||
|
setInternalStateId(0);
|
||||||
|
setInternalMuniId(0);
|
||||||
|
setExternalStateId(0);
|
||||||
|
setExternalMuniId(0);
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const stateOptions = statesData?.data || [];
|
||||||
|
const internalMuniOptions = internalMuniData?.data || [];
|
||||||
|
const internalParishOptions = internalParishData?.data || [];
|
||||||
|
const externalMuniOptions = externalMuniData?.data || [];
|
||||||
|
const externalParishOptions = externalParishData?.data || [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
@@ -68,7 +158,7 @@ export function ProductActivityList() {
|
|||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
<DialogContent className="sm:max-w-[800px] max-h-[80vh] overflow-y-auto">
|
<DialogContent className="sm:max-w-[800px] max-h-[80vh] overflow-y-auto">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Producto Terminado</DialogTitle>
|
<DialogTitle>Detalles de Actividad Productiva</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogDescription className="sr-only">
|
<DialogDescription className="sr-only">
|
||||||
Datos de actividad productiva
|
Datos de actividad productiva
|
||||||
@@ -76,6 +166,15 @@ export function ProductActivityList() {
|
|||||||
<div className="space-y-6 py-4">
|
<div className="space-y-6 py-4">
|
||||||
{/* Basic Info */}
|
{/* Basic Info */}
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Producto Terminado</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.productName}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, productName: e.target.value })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Descripción</Label>
|
<Label>Descripción</Label>
|
||||||
<Input
|
<Input
|
||||||
@@ -120,6 +219,222 @@ export function ProductActivityList() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<h4 className="font-semibold">Distribución Interna</h4>
|
||||||
|
<div className="grid grid-cols-3 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Estado</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={stateOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) => {
|
||||||
|
const id = Number(val);
|
||||||
|
setInternalStateId(id);
|
||||||
|
setNewItem({ ...newItem, internalState: id });
|
||||||
|
}}
|
||||||
|
placeholder="Estado"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Municipio</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={internalMuniOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) => {
|
||||||
|
const id = Number(val);
|
||||||
|
setInternalMuniId(id);
|
||||||
|
setNewItem({ ...newItem, internalMunicipality: id });
|
||||||
|
}}
|
||||||
|
placeholder="Municipio"
|
||||||
|
disabled={!internalStateId}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Parroquia</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={internalParishOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) =>
|
||||||
|
setNewItem({ ...newItem, internalParish: Number(val) })
|
||||||
|
}
|
||||||
|
placeholder="Parroquia"
|
||||||
|
disabled={!internalMuniId}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Breve Descripción</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.internalDescription}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({
|
||||||
|
...newItem,
|
||||||
|
internalDescription: e.target.value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Cantidad Numérica (Kg, TON, UNID. LT)</Label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={newItem.internalQuantity}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({
|
||||||
|
...newItem,
|
||||||
|
internalQuantity: e.target.value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<h4 className="font-semibold">Distribución Externa</h4>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>País</Label>
|
||||||
|
<Select
|
||||||
|
value={newItem.externalCountry}
|
||||||
|
onValueChange={(val) =>
|
||||||
|
setNewItem({ ...newItem, externalCountry: val })
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full">
|
||||||
|
<SelectValue placeholder="Seleccione País" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{/* 3. CORRECCIÓN DEL MAPEO DE PAÍSES Y KEYS */}
|
||||||
|
{COUNTRY_OPTIONS.map((country: string) => (
|
||||||
|
<SelectItem key={country} value={country}>
|
||||||
|
{country}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
{!isVenezuela && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Ciudad</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.externalCity}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, externalCity: e.target.value })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isVenezuela && (
|
||||||
|
<div className="grid grid-cols-3 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Estado</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={stateOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) => {
|
||||||
|
const id = Number(val);
|
||||||
|
setExternalStateId(id);
|
||||||
|
setNewItem({ ...newItem, externalState: id });
|
||||||
|
}}
|
||||||
|
placeholder="Estado"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Municipio</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={externalMuniOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) => {
|
||||||
|
const id = Number(val);
|
||||||
|
setExternalMuniId(id);
|
||||||
|
setNewItem({ ...newItem, externalMunicipality: id });
|
||||||
|
}}
|
||||||
|
placeholder="Municipio"
|
||||||
|
disabled={!externalStateId}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Parroquia</Label>
|
||||||
|
<SelectSearchable
|
||||||
|
options={externalParishOptions.map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
}))}
|
||||||
|
onValueChange={(val) =>
|
||||||
|
setNewItem({ ...newItem, externalParish: Number(val) })
|
||||||
|
}
|
||||||
|
placeholder="Parroquia"
|
||||||
|
disabled={!externalMuniId}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Breve Descripción</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.externalDescription}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({
|
||||||
|
...newItem,
|
||||||
|
externalDescription: e.target.value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Cantidad Numérica (Kg, TON, UNID. LT)</Label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={newItem.externalQuantity}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({
|
||||||
|
...newItem,
|
||||||
|
externalQuantity: e.target.value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<h4 className="font-semibold">Mano de Obra</h4>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Mujer (cantidad)</Label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={newItem.womenCount}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, womenCount: e.target.value })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Hombre (cantidad)</Label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={newItem.menCount}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, menCount: e.target.value })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div className="flex justify-end gap-4">
|
<div className="flex justify-end gap-4">
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@@ -140,10 +455,9 @@ export function ProductActivityList() {
|
|||||||
<Table>
|
<Table>
|
||||||
<TableHeader>
|
<TableHeader>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableHead>Producto/Descripción</TableHead>
|
<TableHead>Producto</TableHead>
|
||||||
<TableHead>Producción Diario</TableHead>
|
<TableHead>Descripción</TableHead>
|
||||||
<TableHead>Producción Semanal</TableHead>
|
<TableHead>Mensual</TableHead>
|
||||||
<TableHead>Producción Mensual</TableHead>
|
|
||||||
<TableHead className="w-[50px]"></TableHead>
|
<TableHead className="w-[50px]"></TableHead>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
@@ -153,28 +467,13 @@ export function ProductActivityList() {
|
|||||||
<TableCell>
|
<TableCell>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
{...register(`productList.${index}.description`)}
|
{...register(`productList.${index}.productName`)}
|
||||||
defaultValue={field.description ?? ''}
|
// field.productName ahora es válido gracias a la interface
|
||||||
|
value={field.productName}
|
||||||
/>
|
/>
|
||||||
<input
|
{field.productName}
|
||||||
type="hidden"
|
|
||||||
{...register(`productList.${index}.dailyCount`)}
|
|
||||||
defaultValue={field.dailyCount ?? ''}
|
|
||||||
/>
|
|
||||||
<input
|
|
||||||
type="hidden"
|
|
||||||
{...register(`productList.${index}.weeklyCount`)}
|
|
||||||
defaultValue={field.weeklyCount ?? ''}
|
|
||||||
/>
|
|
||||||
<input
|
|
||||||
type="hidden"
|
|
||||||
{...register(`productList.${index}.monthlyCount`)}
|
|
||||||
defaultValue={field.monthlyCount ?? ''}
|
|
||||||
/>
|
|
||||||
{field.description}
|
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>{field.dailyCount}</TableCell>
|
<TableCell>{field.description}</TableCell>
|
||||||
<TableCell>{field.weeklyCount}</TableCell>
|
|
||||||
<TableCell>{field.monthlyCount}</TableCell>
|
<TableCell>{field.monthlyCount}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -17,39 +17,27 @@ import {
|
|||||||
} from '@repo/shadcn/components/ui/table';
|
} from '@repo/shadcn/components/ui/table';
|
||||||
import { Input } from '@repo/shadcn/input';
|
import { Input } from '@repo/shadcn/input';
|
||||||
import { Label } from '@repo/shadcn/label';
|
import { Label } from '@repo/shadcn/label';
|
||||||
import {
|
|
||||||
Select,
|
|
||||||
SelectContent,
|
|
||||||
SelectItem,
|
|
||||||
SelectTrigger,
|
|
||||||
SelectValue,
|
|
||||||
} from '@repo/shadcn/select';
|
|
||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2 } from 'lucide-react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||||
import { TrainingSchema } from '../schemas/training';
|
|
||||||
|
|
||||||
const UNIT_OPTIONS = ['KG', 'TON', 'UNID', 'LT', 'MTS', 'QQ', 'HM2', 'SACOS'];
|
|
||||||
|
|
||||||
export function ProductionList() {
|
export function ProductionList() {
|
||||||
const { control, register } = useFormContext<TrainingSchema>();
|
const { control, register } = useFormContext();
|
||||||
const { fields, append, remove } = useFieldArray({
|
const { fields, append, remove } = useFieldArray({
|
||||||
control,
|
control,
|
||||||
name: 'productionList',
|
name: 'productionList',
|
||||||
});
|
});
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [newItem, setNewItem] = useState({
|
const [newItem, setNewItem] = useState({
|
||||||
|
rawMaterial: '',
|
||||||
supplyType: '',
|
supplyType: '',
|
||||||
quantity: '',
|
quantity: '',
|
||||||
unit: '',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleAdd = (e: React.MouseEvent) => {
|
const handleAdd = () => {
|
||||||
e.preventDefault();
|
if (newItem.rawMaterial && newItem.quantity) {
|
||||||
e.stopPropagation();
|
|
||||||
if (newItem.supplyType && newItem.quantity && newItem.unit) {
|
|
||||||
append({ ...newItem, quantity: Number(newItem.quantity) });
|
append({ ...newItem, quantity: Number(newItem.quantity) });
|
||||||
setNewItem({ supplyType: '', quantity: '', unit: '' });
|
setNewItem({ rawMaterial: '', supplyType: '', quantity: '' });
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -62,14 +50,24 @@ export function ProductionList() {
|
|||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<Button variant="outline">Agregar Producción</Button>
|
<Button variant="outline">Agregar Producción</Button>
|
||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
<DialogContent onPointerDownOutside={(e) => e.preventDefault()}>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Materia prima requerida (mensual)</DialogTitle>
|
<DialogTitle>Agregar Datos de Producción</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogDescription className="sr-only">
|
<DialogDescription className="sr-only">
|
||||||
Datos de producción
|
Datos de producción
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Materia prima requerida (mensual)</Label>
|
||||||
|
<Input
|
||||||
|
value={newItem.rawMaterial}
|
||||||
|
onChange={(e) =>
|
||||||
|
setNewItem({ ...newItem, rawMaterial: e.target.value })
|
||||||
|
}
|
||||||
|
placeholder="Descripción de materia prima"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Tipo de Insumo/Rubro</Label>
|
<Label>Tipo de Insumo/Rubro</Label>
|
||||||
<Input
|
<Input
|
||||||
@@ -81,57 +79,26 @@ export function ProductionList() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Cantidad Mensual</Label>
|
<Label>Cantidad Mensual (Kg, TON, UNID. LT)</Label>
|
||||||
<div className="flex gap-2">
|
<Input
|
||||||
<Input
|
type="number"
|
||||||
type="number"
|
value={newItem.quantity}
|
||||||
className="flex-1"
|
onChange={(e) =>
|
||||||
value={newItem.quantity}
|
setNewItem({ ...newItem, quantity: e.target.value })
|
||||||
onChange={(e) =>
|
}
|
||||||
setNewItem({ ...newItem, quantity: e.target.value })
|
placeholder="0"
|
||||||
}
|
/>
|
||||||
placeholder="0"
|
|
||||||
/>
|
|
||||||
<Select
|
|
||||||
value={newItem.unit}
|
|
||||||
onValueChange={(val) =>
|
|
||||||
setNewItem({ ...newItem, unit: val })
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<SelectTrigger className="w-[120px]">
|
|
||||||
<SelectValue placeholder="Unidad" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
{UNIT_OPTIONS.map((unit) => (
|
|
||||||
<SelectItem key={unit} value={unit}>
|
|
||||||
{unit}
|
|
||||||
</SelectItem>
|
|
||||||
))}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-end gap-4">
|
<div className="flex justify-end gap-4">
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
type="button"
|
type="button"
|
||||||
onClick={(e) => {
|
onClick={() => setIsOpen(false)}
|
||||||
e.preventDefault();
|
|
||||||
setIsOpen(false);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
Cancelar
|
Cancelar
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
<Button onClick={handleAdd}>Guardar</Button>
|
||||||
type="button"
|
|
||||||
onClick={handleAdd}
|
|
||||||
disabled={
|
|
||||||
!newItem.supplyType || !newItem.quantity || !newItem.unit
|
|
||||||
}
|
|
||||||
>
|
|
||||||
Guardar
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
@@ -142,6 +109,7 @@ export function ProductionList() {
|
|||||||
<Table>
|
<Table>
|
||||||
<TableHeader>
|
<TableHeader>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
|
<TableHead>Materia Prima</TableHead>
|
||||||
<TableHead>Tipo Insumo</TableHead>
|
<TableHead>Tipo Insumo</TableHead>
|
||||||
<TableHead>Cantidad (Mensual)</TableHead>
|
<TableHead>Cantidad (Mensual)</TableHead>
|
||||||
<TableHead className="w-[50px]"></TableHead>
|
<TableHead className="w-[50px]"></TableHead>
|
||||||
@@ -153,33 +121,32 @@ export function ProductionList() {
|
|||||||
<TableCell>
|
<TableCell>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
{...register(`productionList.${index}.supplyType`)}
|
{...register(`productionList.${index}.rawMaterial`)}
|
||||||
defaultValue={field.supplyType ?? ''}
|
|
||||||
/>
|
/>
|
||||||
|
{/* @ts-ignore */}
|
||||||
|
{field.rawMaterial}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
{...register(`productionList.${index}.supplyType`)}
|
||||||
|
/>
|
||||||
|
{/* @ts-ignore */}
|
||||||
{field.supplyType}
|
{field.supplyType}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
{...register(`productionList.${index}.quantity`)}
|
{...register(`productionList.${index}.quantity`)}
|
||||||
defaultValue={field.quantity ?? ''}
|
|
||||||
/>
|
/>
|
||||||
<input
|
{/* @ts-ignore */}
|
||||||
type="hidden"
|
{field.quantity}
|
||||||
{...register(`productionList.${index}.unit`)}
|
|
||||||
defaultValue={field.unit ?? ''}
|
|
||||||
/>
|
|
||||||
{field.quantity} {field.unit}
|
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
type="button"
|
onClick={() => remove(index)}
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
remove(index);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<Trash2 className="h-4 w-4 text-destructive" />
|
<Trash2 className="h-4 w-4 text-destructive" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -29,18 +29,10 @@ export default function TrainingList({
|
|||||||
return <DataTableSkeleton columnCount={5} rowCount={initialLimit} />;
|
return <DataTableSkeleton columnCount={5} rowCount={initialLimit} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
const transformedData =
|
|
||||||
data?.data?.map((item) => ({
|
|
||||||
...item,
|
|
||||||
communeRif: item.communeRif || '',
|
|
||||||
communeSpokespersonName: item.communeSpokespersonName || '',
|
|
||||||
communalCouncilRif: item.communalCouncilRif || '',
|
|
||||||
})) || [];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataTable
|
<DataTable
|
||||||
columns={columns({ apiUrl })}
|
columns={columns({ apiUrl })}
|
||||||
data={transformedData}
|
data={data?.data || []}
|
||||||
totalItems={data?.meta.totalCount || 0}
|
totalItems={data?.meta.totalCount || 0}
|
||||||
pageSizeOptions={[10, 20, 30, 40, 50]}
|
pageSizeOptions={[10, 20, 30, 40, 50]}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ import {
|
|||||||
TooltipProvider,
|
TooltipProvider,
|
||||||
TooltipTrigger,
|
TooltipTrigger,
|
||||||
} from '@repo/shadcn/tooltip';
|
} from '@repo/shadcn/tooltip';
|
||||||
import { Edit, Eye, Trash } from 'lucide-react';
|
import { Edit, Eye, Trash, FileDown } from 'lucide-react';
|
||||||
import { useSession } from 'next-auth/react';
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { TrainingViewModal } from '../training-view-modal';
|
import { TrainingViewModal } from '../training-view-modal';
|
||||||
@@ -26,7 +25,6 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
|||||||
const [viewOpen, setViewOpen] = useState(false);
|
const [viewOpen, setViewOpen] = useState(false);
|
||||||
const { mutate: deleteTraining } = useDeleteTraining();
|
const { mutate: deleteTraining } = useDeleteTraining();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data: session } = useSession();
|
|
||||||
|
|
||||||
const onConfirm = async () => {
|
const onConfirm = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -40,29 +38,9 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mapear roles a minúsculas para comparación segura
|
const handleExport = (id?: number | undefined) => {
|
||||||
const userRoles = session?.user?.role?.map((r) => r.rol.toLowerCase()) || [];
|
window.open(`${apiUrl}/training/export/${id}`, '_blank');
|
||||||
|
};
|
||||||
const isAdminOrSuper = userRoles.some((r) =>
|
|
||||||
['superadmin', 'admin'].includes(r),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Soporta tanto 'coordinator' como 'coordinador'
|
|
||||||
const isCoordinator = userRoles.some(r =>
|
|
||||||
r.includes('coordinator') || r.includes('coordinador')
|
|
||||||
);
|
|
||||||
|
|
||||||
const isOtherAuthorized = userRoles.some((r) =>
|
|
||||||
['autoridad', 'manager'].includes(r),
|
|
||||||
);
|
|
||||||
|
|
||||||
// El creador del registro: intentamos createdBy o created_by por si acaso
|
|
||||||
const createdBy = data.createdBy ?? (data as any).created_by;
|
|
||||||
|
|
||||||
// Comparación robusta de IDs
|
|
||||||
const isOwner = createdBy !== undefined &&
|
|
||||||
createdBy !== null &&
|
|
||||||
Number(createdBy) === Number(session?.user?.id);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -82,67 +60,75 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="flex gap-1">
|
<div className="flex gap-1">
|
||||||
{/* VER DETALLE: superadmin, admin, autoridad, manager, or owner coordinator */}
|
<TooltipProvider>
|
||||||
{(isAdminOrSuper || isOtherAuthorized || (isCoordinator && isOwner)) && (
|
<Tooltip>
|
||||||
<TooltipProvider>
|
<TooltipTrigger asChild>
|
||||||
<Tooltip>
|
<Button
|
||||||
<TooltipTrigger asChild>
|
variant="outline"
|
||||||
<Button
|
size="icon"
|
||||||
variant="outline"
|
onClick={() => setViewOpen(true)}
|
||||||
size="icon"
|
>
|
||||||
onClick={() => setViewOpen(true)}
|
<Eye className="h-4 w-4" />
|
||||||
>
|
</Button>
|
||||||
<Eye className="h-4 w-4" />
|
</TooltipTrigger>
|
||||||
</Button>
|
<TooltipContent>
|
||||||
</TooltipTrigger>
|
<p>Ver detalle</p>
|
||||||
<TooltipContent>
|
</TooltipContent>
|
||||||
<p>Ver detalle</p>
|
</Tooltip>
|
||||||
</TooltipContent>
|
</TooltipProvider>
|
||||||
</Tooltip>
|
|
||||||
</TooltipProvider>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* EDITAR: Superadmin, admin OR (coordinator if owner) */}
|
<TooltipProvider>
|
||||||
{(isAdminOrSuper || (isCoordinator && isOwner)) && (
|
<Tooltip>
|
||||||
<TooltipProvider>
|
<TooltipTrigger asChild>
|
||||||
<Tooltip>
|
<Button
|
||||||
<TooltipTrigger asChild>
|
variant="outline"
|
||||||
<Button
|
size="icon"
|
||||||
variant="outline"
|
onClick={() => handleExport(data.id)}
|
||||||
size="icon"
|
>
|
||||||
onClick={() =>
|
<FileDown className="h-4 w-4" />
|
||||||
router.push(`/dashboard/formulario/editar/${data.id}`)
|
</Button>
|
||||||
}
|
</TooltipTrigger>
|
||||||
>
|
<TooltipContent>
|
||||||
<Edit className="h-4 w-4" />
|
<p>Exportar Excel</p>
|
||||||
</Button>
|
</TooltipContent>
|
||||||
</TooltipTrigger>
|
</Tooltip>
|
||||||
<TooltipContent>
|
</TooltipProvider>
|
||||||
<p>Editar</p>
|
|
||||||
</TooltipContent>
|
|
||||||
</Tooltip>
|
|
||||||
</TooltipProvider>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* ELIMINAR: Solo superadmin y admin */}
|
<TooltipProvider>
|
||||||
{isAdminOrSuper && (
|
<Tooltip>
|
||||||
<TooltipProvider>
|
<TooltipTrigger asChild>
|
||||||
<Tooltip>
|
<Button
|
||||||
<TooltipTrigger asChild>
|
variant="outline"
|
||||||
<Button
|
size="icon"
|
||||||
variant="outline"
|
onClick={() =>
|
||||||
size="icon"
|
router.push(`/dashboard/formulario/editar/${data.id}`)
|
||||||
onClick={() => setOpen(true)}
|
}
|
||||||
>
|
>
|
||||||
<Trash className="h-4 w-4" />
|
<Edit className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
</TooltipTrigger>
|
</TooltipTrigger>
|
||||||
<TooltipContent>
|
<TooltipContent>
|
||||||
<p>Eliminar</p>
|
<p>Editar</p>
|
||||||
</TooltipContent>
|
</TooltipContent>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
</TooltipProvider>
|
</TooltipProvider>
|
||||||
)}
|
|
||||||
|
<TooltipProvider>
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="icon"
|
||||||
|
onClick={() => setOpen(true)}
|
||||||
|
>
|
||||||
|
<Trash className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<p>Eliminar</p>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -22,15 +22,6 @@ export function columns({ apiUrl }: ColumnsProps): ColumnDef<TrainingSchema>[] {
|
|||||||
accessorKey: 'ospType',
|
accessorKey: 'ospType',
|
||||||
header: 'Tipo',
|
header: 'Tipo',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
accessorKey: 'created_at',
|
|
||||||
header: 'Fecha de creación',
|
|
||||||
cell: ({ row }) => {
|
|
||||||
// console.log(row.getValue('created_at'));
|
|
||||||
const date = row.getValue('created_at') as string;
|
|
||||||
return date ? new Date(date).toLocaleString() : 'N/A';
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
accessorKey: 'currentStatus',
|
accessorKey: 'currentStatus',
|
||||||
header: 'Estatus',
|
header: 'Estatus',
|
||||||
|
|||||||
@@ -3,15 +3,12 @@
|
|||||||
import { Button } from '@repo/shadcn/components/ui/button';
|
import { Button } from '@repo/shadcn/components/ui/button';
|
||||||
import { DataTableSearch } from '@repo/shadcn/table/data-table-search';
|
import { DataTableSearch } from '@repo/shadcn/table/data-table-search';
|
||||||
import { Plus } from 'lucide-react';
|
import { Plus } from 'lucide-react';
|
||||||
import { useSession } from 'next-auth/react';
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useTrainingTableFilters } from './use-training-table-filters';
|
import { useTrainingTableFilters } from './use-training-table-filters';
|
||||||
|
|
||||||
export default function TrainingTableAction() {
|
export default function TrainingTableAction() {
|
||||||
const { searchQuery, setPage, setSearchQuery } = useTrainingTableFilters();
|
const { searchQuery, setPage, setSearchQuery } = useTrainingTableFilters();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data: session } = useSession();
|
|
||||||
const role = session?.user.role[0]?.rol;
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center justify-between mt-4 ">
|
<div className="flex items-center justify-between mt-4 ">
|
||||||
<div className="flex items-center gap-4 flex-grow">
|
<div className="flex items-center gap-4 flex-grow">
|
||||||
@@ -22,17 +19,13 @@ export default function TrainingTableAction() {
|
|||||||
setPage={setPage}
|
setPage={setPage}
|
||||||
/>
|
/>
|
||||||
</div>{' '}
|
</div>{' '}
|
||||||
{['superadmin', 'autoridad', 'admin', 'manager', 'coordinators'].includes(
|
<Button
|
||||||
role ?? '',
|
onClick={() => router.push(`/dashboard/formulario/nuevo`)}
|
||||||
) && (
|
size="sm"
|
||||||
<Button
|
>
|
||||||
onClick={() => router.push(`/dashboard/formulario/nuevo`)}
|
<Plus className="h-4 w-4" />
|
||||||
size="sm"
|
<span className="hidden md:inline">Nuevo Registro</span>
|
||||||
>
|
</Button>
|
||||||
<Plus className="h-4 w-4" />
|
|
||||||
<span className="hidden md:inline">Nuevo Registro</span>
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import {
|
|
||||||
useMunicipalityQuery,
|
|
||||||
useParishQuery,
|
|
||||||
useStateQuery,
|
|
||||||
} from '@/feactures/location/hooks/use-query-location';
|
|
||||||
import { Badge } from '@repo/shadcn/badge';
|
import { Badge } from '@repo/shadcn/badge';
|
||||||
import { Button } from '@repo/shadcn/button';
|
import { Button } from '@repo/shadcn/button';
|
||||||
import {
|
import {
|
||||||
@@ -22,6 +17,7 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from '@repo/shadcn/components/ui/dialog';
|
} from '@repo/shadcn/components/ui/dialog';
|
||||||
import { ScrollArea } from '@repo/shadcn/components/ui/scroll-area';
|
import { ScrollArea } from '@repo/shadcn/components/ui/scroll-area';
|
||||||
|
import { Separator } from '@repo/shadcn/components/ui/separator';
|
||||||
import {
|
import {
|
||||||
ExternalLink,
|
ExternalLink,
|
||||||
Factory,
|
Factory,
|
||||||
@@ -32,6 +28,11 @@ import {
|
|||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { TrainingSchema } from '../schemas/training';
|
import { TrainingSchema } from '../schemas/training';
|
||||||
|
import {
|
||||||
|
useMunicipalityQuery,
|
||||||
|
useParishQuery,
|
||||||
|
useStateQuery,
|
||||||
|
} from '@/feactures/location/hooks/use-query-location';
|
||||||
|
|
||||||
interface TrainingViewModalProps {
|
interface TrainingViewModalProps {
|
||||||
data: TrainingSchema | null;
|
data: TrainingSchema | null;
|
||||||
@@ -45,16 +46,14 @@ export function TrainingViewModal({
|
|||||||
onClose,
|
onClose,
|
||||||
}: TrainingViewModalProps) {
|
}: TrainingViewModalProps) {
|
||||||
const [selectedImage, setSelectedImage] = useState<string | null>(null);
|
const [selectedImage, setSelectedImage] = useState<string | null>(null);
|
||||||
|
|
||||||
const { data: statesData } = useStateQuery();
|
const { data: statesData } = useStateQuery();
|
||||||
const { data: municipalitiesData } = useMunicipalityQuery(data?.state || 0);
|
const { data: municipalitiesData } = useMunicipalityQuery(data?.state || 0);
|
||||||
const { data: parishesData } = useParishQuery(data?.municipality || 0);
|
const { data: parishesData } = useParishQuery(data?.municipality || 0);
|
||||||
|
|
||||||
if (!data) return null;
|
if (!data) return null;
|
||||||
|
|
||||||
const stateName = statesData?.data?.find(
|
const stateName = statesData?.data?.find((s: any) => s.id === data.state)?.name;
|
||||||
(s: any) => s.id === data.state,
|
|
||||||
)?.name;
|
|
||||||
const municipalityName = municipalitiesData?.data?.find(
|
const municipalityName = municipalitiesData?.data?.find(
|
||||||
(m: any) => m.id === data.municipality,
|
(m: any) => m.id === data.municipality,
|
||||||
)?.name;
|
)?.name;
|
||||||
@@ -95,15 +94,12 @@ export function TrainingViewModal({
|
|||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|
||||||
const BooleanBadge = ({ value }: { value?: boolean | null }) => (
|
const BooleanBadge = ({ value }: { value?: boolean }) => (
|
||||||
<Badge variant={value ? 'default' : 'secondary'}>
|
<Badge variant={value ? 'default' : 'secondary'}>
|
||||||
{value ? 'Sí' : 'No'}
|
{value ? 'Sí' : 'No'}
|
||||||
</Badge>
|
</Badge>
|
||||||
);
|
);
|
||||||
|
|
||||||
// console.log(data);
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||||
@@ -131,7 +127,10 @@ export function TrainingViewModal({
|
|||||||
<div className="space-y-8">
|
<div className="space-y-8">
|
||||||
{/* 1. Datos de la Visita */}
|
{/* 1. Datos de la Visita */}
|
||||||
<Section title="Datos de la Visita">
|
<Section title="Datos de la Visita">
|
||||||
<DetailItem label="Coordinador" value={data.coorFullName} />
|
<DetailItem
|
||||||
|
label="Coordinador"
|
||||||
|
value={`${data.firstname} ${data.lastname}`}
|
||||||
|
/>
|
||||||
<DetailItem label="Teléfono Coord." value={data.coorPhone} />
|
<DetailItem label="Teléfono Coord." value={data.coorPhone} />
|
||||||
<DetailItem
|
<DetailItem
|
||||||
label="Fecha Visita"
|
label="Fecha Visita"
|
||||||
@@ -161,17 +160,12 @@ export function TrainingViewModal({
|
|||||||
label="Actividad Principal"
|
label="Actividad Principal"
|
||||||
value={data.mainProductiveActivity}
|
value={data.mainProductiveActivity}
|
||||||
/>
|
/>
|
||||||
{/* <div className="sm-col-span-full"> */}
|
<div className="col-span-full">
|
||||||
<DetailItem
|
<DetailItem
|
||||||
label="Actividad Específica"
|
label="Actividad Específica"
|
||||||
value={data.productiveActivity}
|
value={data.productiveActivity}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
{data.productiveActivity == 'OTRO' && (<DetailItem
|
|
||||||
label="Otra Actividad Específica"
|
|
||||||
value={data.productiveActivityOther}
|
|
||||||
/>)}
|
|
||||||
{/* </div> */}
|
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
{/* 3. Infraestructura y Ubicación */}
|
{/* 3. Infraestructura y Ubicación */}
|
||||||
@@ -213,11 +207,7 @@ export function TrainingViewModal({
|
|||||||
className="gap-2"
|
className="gap-2"
|
||||||
>
|
>
|
||||||
<a
|
<a
|
||||||
href={
|
href={data.ospGoogleMapsLink}
|
||||||
data.ospGoogleMapsLink.startsWith('http')
|
|
||||||
? data.ospGoogleMapsLink
|
|
||||||
: `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(data.ospGoogleMapsLink)}`
|
|
||||||
}
|
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noreferrer"
|
rel="noreferrer"
|
||||||
>
|
>
|
||||||
@@ -237,36 +227,74 @@ export function TrainingViewModal({
|
|||||||
<CardHeader className="pb-3">
|
<CardHeader className="pb-3">
|
||||||
<CardTitle className="text-lg flex items-center gap-2">
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
<Package className="h-5 w-5" />
|
<Package className="h-5 w-5" />
|
||||||
Productos Registrados
|
Productos y Mano de Obra
|
||||||
<Badge variant="secondary" className="ml-2">
|
<Badge variant="secondary" className="ml-2">
|
||||||
{data.productList?.length || 0}
|
{data.productList?.length || 0}
|
||||||
</Badge>
|
</Badge>
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="grid gap-4">
|
<CardContent className="grid gap-4">
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
{data.productList?.map((prod: any, idx: number) => (
|
||||||
{data.productList?.map((prod: any, idx: number) => (
|
<div
|
||||||
<div
|
key={idx}
|
||||||
key={idx}
|
className="bg-muted/40 p-4 rounded-lg border text-sm"
|
||||||
className="bg-muted/40 p-4 rounded-lg border text-sm"
|
>
|
||||||
>
|
<div className="flex justify-between items-start mb-2">
|
||||||
<h4 className="font-bold text-base text-primary mb-2">
|
<h4 className="font-bold text-base text-primary">
|
||||||
{prod.description}
|
{prod.productName}
|
||||||
</h4>
|
</h4>
|
||||||
<div className="grid grid-cols-3 gap-2">
|
<Badge variant="outline">
|
||||||
<DetailItem label="Diario" value={prod.dailyCount} />
|
Mano de obra:{' '}
|
||||||
<DetailItem
|
{Number(prod.menCount || 0) +
|
||||||
label="Semanal"
|
Number(prod.womenCount || 0)}
|
||||||
value={prod.weeklyCount}
|
</Badge>
|
||||||
/>
|
|
||||||
<DetailItem
|
|
||||||
label="Mensual"
|
|
||||||
value={prod.monthlyCount}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
))}
|
<p className="text-muted-foreground mb-3">
|
||||||
</div>
|
{prod.description}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-3">
|
||||||
|
<DetailItem label="Diario" value={prod.dailyCount} />
|
||||||
|
<DetailItem label="Semanal" value={prod.weeklyCount} />
|
||||||
|
<DetailItem label="Mensual" value={prod.monthlyCount} />
|
||||||
|
<DetailItem
|
||||||
|
label="Hombres / Mujeres"
|
||||||
|
value={`${prod.menCount || 0} / ${prod.womenCount || 0}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Detalles de distribución si existen */}
|
||||||
|
{(prod.internalQuantity || prod.externalQuantity) && (
|
||||||
|
<>
|
||||||
|
<Separator className="my-2" />
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
|
{prod.internalQuantity && (
|
||||||
|
<div>
|
||||||
|
<span className="text-xs font-bold text-muted-foreground block mb-1">
|
||||||
|
DISTRIBUCIÓN INTERNA
|
||||||
|
</span>
|
||||||
|
<p>Cant: {prod.internalQuantity}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{prod.internalDescription}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{prod.externalQuantity && (
|
||||||
|
<div>
|
||||||
|
<span className="text-xs font-bold text-muted-foreground block mb-1">
|
||||||
|
EXPORTACIÓN ({prod.externalCountry})
|
||||||
|
</span>
|
||||||
|
<p>Cant: {prod.externalQuantity}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{prod.externalDescription}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
{(!data.productList || data.productList.length === 0) && (
|
{(!data.productList || data.productList.length === 0) && (
|
||||||
<p className="text-sm text-muted-foreground italic">
|
<p className="text-sm text-muted-foreground italic">
|
||||||
No hay productos registrados.
|
No hay productos registrados.
|
||||||
@@ -275,64 +303,6 @@ export function TrainingViewModal({
|
|||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* DISTRIBUCIÓN, EXPORTACIÓN Y MANO DE OBRA */}
|
|
||||||
<Section title="Distribución, Exportación y Mano de Obra">
|
|
||||||
<div className="col-span-full grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
|
||||||
<div className="space-y-4">
|
|
||||||
<h4 className="text-sm font-bold border-b pb-1">
|
|
||||||
Distribución Interna
|
|
||||||
</h4>
|
|
||||||
<DetailItem
|
|
||||||
label="Zona de Distribución"
|
|
||||||
value={data.internalDistributionZone}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-4">
|
|
||||||
<h4 className="text-sm font-bold border-b pb-1">
|
|
||||||
Mano de Obra
|
|
||||||
</h4>
|
|
||||||
<div className="grid grid-cols-2 gap-4">
|
|
||||||
<DetailItem label="Mujeres" value={data.womenCount} />
|
|
||||||
<DetailItem label="Hombres" value={data.menCount} />
|
|
||||||
<DetailItem
|
|
||||||
label="Total"
|
|
||||||
value={
|
|
||||||
Number(data.womenCount || 0) +
|
|
||||||
Number(data.menCount || 0)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-4">
|
|
||||||
<h4 className="text-sm font-bold border-b pb-1 flex items-center gap-2">
|
|
||||||
Exportación <BooleanBadge value={data.isExporting} />
|
|
||||||
</h4>
|
|
||||||
{data.isExporting && (
|
|
||||||
<div className="space-y-3">
|
|
||||||
<DetailItem label="País" value={data.externalCountry} />
|
|
||||||
<DetailItem label="Ciudad" value={data.externalCity} />
|
|
||||||
<DetailItem
|
|
||||||
label="Descripción"
|
|
||||||
value={data.externalDescription}
|
|
||||||
/>
|
|
||||||
<div className="grid grid-cols-2 gap-2">
|
|
||||||
<DetailItem
|
|
||||||
label="Cantidad"
|
|
||||||
value={data.externalQuantity}
|
|
||||||
/>
|
|
||||||
<DetailItem
|
|
||||||
label="Unidad"
|
|
||||||
value={data.externalUnit}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Section>
|
|
||||||
|
|
||||||
{/* EQUIPAMIENTO Y PRODUCCIÓN */}
|
{/* EQUIPAMIENTO Y PRODUCCIÓN */}
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
<Card>
|
<Card>
|
||||||
@@ -390,9 +360,7 @@ export function TrainingViewModal({
|
|||||||
{mat.supplyType}
|
{mat.supplyType}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Badge variant="secondary">
|
<Badge variant="secondary">Cant: {mat.quantity}</Badge>
|
||||||
Cant: {mat.quantity} {mat.unit}
|
|
||||||
</Badge>
|
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
{(!data.productionList ||
|
{(!data.productionList ||
|
||||||
@@ -446,12 +414,12 @@ export function TrainingViewModal({
|
|||||||
label="Teléfono"
|
label="Teléfono"
|
||||||
value={data.ospResponsiblePhone}
|
value={data.ospResponsiblePhone}
|
||||||
/>
|
/>
|
||||||
{/* <DetailItem label="Email" value={data.ospResponsibleEmail} />
|
<DetailItem label="Email" value={data.ospResponsibleEmail} />
|
||||||
<DetailItem
|
<DetailItem
|
||||||
label="Carga Familiar"
|
label="Carga Familiar"
|
||||||
value={data.familyBurden}
|
value={data.familyBurden}
|
||||||
/>
|
/>
|
||||||
<DetailItem label="Hijos" value={data.numberOfChildren} /> */}
|
<DetailItem label="Hijos" value={data.numberOfChildren} />
|
||||||
</Section>
|
</Section>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -495,7 +463,7 @@ export function TrainingViewModal({
|
|||||||
onClick={() => setSelectedImage(photo)}
|
onClick={() => setSelectedImage(photo)}
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={`${photo}`}
|
src={`${process.env.NEXT_PUBLIC_API_URL}${photo}`}
|
||||||
alt={`Evidencia ${idx + 1}`}
|
alt={`Evidencia ${idx + 1}`}
|
||||||
className="object-cover w-full h-full"
|
className="object-cover w-full h-full"
|
||||||
/>
|
/>
|
||||||
@@ -545,7 +513,7 @@ export function TrainingViewModal({
|
|||||||
</Button>
|
</Button>
|
||||||
{selectedImage && (
|
{selectedImage && (
|
||||||
<img
|
<img
|
||||||
src={`${selectedImage}`}
|
src={`${process.env.NEXT_PUBLIC_API_URL}${selectedImage}`}
|
||||||
alt="Vista ampliada"
|
alt="Vista ampliada"
|
||||||
className="max-w-full max-h-[90vh] object-contain rounded-md"
|
className="max-w-full max-h-[90vh] object-contain rounded-md"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export const ACTIVIDAD_PRINCIPAL = {
|
|||||||
PATIOS_PRODUCTIVOS: 'PATIOS PRODUCTIVOS O CONUCOS',
|
PATIOS_PRODUCTIVOS: 'PATIOS PRODUCTIVOS O CONUCOS',
|
||||||
TRANSFORMACION_MATERIA: 'TRANSFORMACION DE LA MATERIA PRIMA',
|
TRANSFORMACION_MATERIA: 'TRANSFORMACION DE LA MATERIA PRIMA',
|
||||||
TEXTIL: 'TALLER DE COFECCION TEXTIL',
|
TEXTIL: 'TALLER DE COFECCION TEXTIL',
|
||||||
CONSTRUCCION: 'CONSTRUCCION',
|
CONSTRUCCION: 'CONSTRUCION',
|
||||||
BIENES_SERVICIOS: 'OFRECER PRODUCTOS DE BIENES Y SERVICIOS',
|
BIENES_SERVICIOS: 'OFRECER PRODUCTOS DE BIENES Y SERVICIOS',
|
||||||
VISITAS_GUIADAS: 'VISITAS GUIADAS',
|
VISITAS_GUIADAS: 'VISITAS GUIADAS',
|
||||||
ALOJAMIENTO: 'ALOJAMIENTO',
|
ALOJAMIENTO: 'ALOJAMIENTO',
|
||||||
@@ -107,7 +107,6 @@ export const ACTIVIDAD_PRODUCTIVA_MAP: Record<string, string[]> = {
|
|||||||
'SIEMBRA DE ARROZ',
|
'SIEMBRA DE ARROZ',
|
||||||
'SIEMBRA DE CEREALES (CEBADA, LINAZA, SOYA)',
|
'SIEMBRA DE CEREALES (CEBADA, LINAZA, SOYA)',
|
||||||
'ELABORACION DE BIO-INSUMO (ABONO ORGANICO)',
|
'ELABORACION DE BIO-INSUMO (ABONO ORGANICO)',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.CRIA]: [
|
[ACTIVIDAD_PRINCIPAL.CRIA]: [
|
||||||
'BOVINO',
|
'BOVINO',
|
||||||
@@ -116,9 +115,8 @@ export const ACTIVIDAD_PRODUCTIVA_MAP: Record<string, string[]> = {
|
|||||||
'CUNICULTURA',
|
'CUNICULTURA',
|
||||||
'AVICOLA',
|
'AVICOLA',
|
||||||
'PISCICULA',
|
'PISCICULA',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.PATIOS_PRODUCTIVOS]: ['SIEMBRA Y CRIA', 'OTRO'],
|
[ACTIVIDAD_PRINCIPAL.PATIOS_PRODUCTIVOS]: ['SIEMBRA Y CRIA'],
|
||||||
[ACTIVIDAD_PRINCIPAL.TRANSFORMACION_MATERIA]: [
|
[ACTIVIDAD_PRINCIPAL.TRANSFORMACION_MATERIA]: [
|
||||||
'ELABORACION DE PRODUCTOS QUIMICOS (LIMPIEZA E HIGIENE PERSONAL)',
|
'ELABORACION DE PRODUCTOS QUIMICOS (LIMPIEZA E HIGIENE PERSONAL)',
|
||||||
'PANADERIAS',
|
'PANADERIAS',
|
||||||
@@ -130,10 +128,7 @@ export const ACTIVIDAD_PRODUCTIVA_MAP: Record<string, string[]> = {
|
|||||||
'ELABORACION DE ACEITE COMESTIBLE',
|
'ELABORACION DE ACEITE COMESTIBLE',
|
||||||
'FABRICA DE HIELO',
|
'FABRICA DE HIELO',
|
||||||
'ELABORACION DE PAPELON',
|
'ELABORACION DE PAPELON',
|
||||||
'TORREFACTORA DE CÁFE',
|
|
||||||
'ESPULPADORA DE TOMATES Y FRUTAS',
|
|
||||||
'ARTESANIAS',
|
'ARTESANIAS',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.TEXTIL]: [
|
[ACTIVIDAD_PRINCIPAL.TEXTIL]: [
|
||||||
'ELABORACION DE UNIFORME ESCOLARES Y PRENDA DE VESTIR',
|
'ELABORACION DE UNIFORME ESCOLARES Y PRENDA DE VESTIR',
|
||||||
@@ -141,14 +136,12 @@ export const ACTIVIDAD_PRODUCTIVA_MAP: Record<string, string[]> = {
|
|||||||
'ELABORACION DE LENCERIA',
|
'ELABORACION DE LENCERIA',
|
||||||
'SUBLIMACION DE TEJIDOS',
|
'SUBLIMACION DE TEJIDOS',
|
||||||
'ELABORACION DE CALZADOS',
|
'ELABORACION DE CALZADOS',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.CONSTRUCCION]: [
|
[ACTIVIDAD_PRINCIPAL.CONSTRUCCION]: [
|
||||||
'BLOQUERAS',
|
'BLOQUERAS',
|
||||||
'PLANTA PREMEZCLADORA DE CEMENTO',
|
'PLANTA PREMEZCLADORA DE CEMENTO',
|
||||||
'CARPINTERIAS',
|
'CARPINTERIAS',
|
||||||
'HERRERIAS',
|
'HERRERIAS',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.BIENES_SERVICIOS]: [
|
[ACTIVIDAD_PRINCIPAL.BIENES_SERVICIOS]: [
|
||||||
'MERCADOS COMUNALES',
|
'MERCADOS COMUNALES',
|
||||||
@@ -162,17 +155,15 @@ export const ACTIVIDAD_PRODUCTIVA_MAP: Record<string, string[]> = {
|
|||||||
'REPARACION DE CALZADOS',
|
'REPARACION DE CALZADOS',
|
||||||
'TALLER DE MECANICA',
|
'TALLER DE MECANICA',
|
||||||
'TRANSPORTES',
|
'TRANSPORTES',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
[ACTIVIDAD_PRINCIPAL.VISITAS_GUIADAS]: ['RUTAS TURISTICAS', 'OTRO'],
|
[ACTIVIDAD_PRINCIPAL.VISITAS_GUIADAS]: ['RUTAS TURISTICAS'],
|
||||||
[ACTIVIDAD_PRINCIPAL.ALOJAMIENTO]: ['POSADAS', 'HOTELES', 'OTRO'],
|
[ACTIVIDAD_PRINCIPAL.ALOJAMIENTO]: ['POSADAS', 'HOTELES'],
|
||||||
[ACTIVIDAD_PRINCIPAL.TURISMO]: ['AGENCIAS DE VIAJES', 'OTRO'],
|
[ACTIVIDAD_PRINCIPAL.TURISMO]: ['AGENCIAS DE VIAJES'],
|
||||||
[ACTIVIDAD_PRINCIPAL.COMERCIO]: [
|
[ACTIVIDAD_PRINCIPAL.COMERCIO]: [
|
||||||
'VENTA DE VIVERES',
|
'VENTA DE VIVERES',
|
||||||
'VENTAS DE PRENDAS DE VESTIR',
|
'VENTAS DE PRENDAS DE VESTIR',
|
||||||
'VENTA DE PRODUCTOS QUIMICOS Y DERIVADOS',
|
'VENTA DE PRODUCTOS QUIMICOS Y DERIVADOS',
|
||||||
'BODEGAS COMUNALES',
|
'BODEGAS COMUNALES',
|
||||||
'FRIGORIFICOS Y CARNICOS',
|
'FRIGORIFICOS Y CARNICOS',
|
||||||
'OTRO'
|
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,79 +3,82 @@ import { z } from 'zod';
|
|||||||
// 1. Definimos el esquema de un item individual de la lista de productos
|
// 1. Definimos el esquema de un item individual de la lista de productos
|
||||||
// Basado en los campos que usaste en ProductActivityList
|
// Basado en los campos que usaste en ProductActivityList
|
||||||
const productItemSchema = z.object({
|
const productItemSchema = z.object({
|
||||||
description: z.string().optional().nullable(),
|
productName: z.string(),
|
||||||
dailyCount: z.coerce.string().or(z.number()).optional().nullable(),
|
description: z.string().optional(),
|
||||||
weeklyCount: z.coerce.string().or(z.number()).optional().nullable(),
|
dailyCount: z.coerce.string().or(z.number()).optional(), // Aceptamos string o number por los inputs
|
||||||
monthlyCount: z.coerce.string().or(z.number()).optional().nullable(),
|
weeklyCount: z.coerce.string().or(z.number()).optional(),
|
||||||
|
monthlyCount: z.coerce.string().or(z.number()).optional(),
|
||||||
|
|
||||||
|
// Distribución Interna
|
||||||
|
internalState: z.number().optional(),
|
||||||
|
internalMunicipality: z.number().optional(),
|
||||||
|
internalParish: z.number().optional(),
|
||||||
|
internalDescription: z.string().optional(),
|
||||||
|
internalQuantity: z.coerce.string().or(z.number()).optional(),
|
||||||
|
|
||||||
|
// Distribución Externa
|
||||||
|
externalCountry: z.string().optional(),
|
||||||
|
externalState: z.number().optional(),
|
||||||
|
externalMunicipality: z.number().optional(),
|
||||||
|
externalParish: z.number().optional(),
|
||||||
|
externalCity: z.string().optional(),
|
||||||
|
externalDescription: z.string().optional(),
|
||||||
|
externalQuantity: z.coerce.string().or(z.number()).optional(),
|
||||||
|
|
||||||
|
// Mano de obra
|
||||||
|
womenCount: z.coerce.string().or(z.number()).optional(),
|
||||||
|
menCount: z.coerce.string().or(z.number()).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const productionItemSchema = z.object({
|
const productionItemSchema = z.object({
|
||||||
supplyType: z.string().optional().nullable(),
|
rawMaterial: z.string(),
|
||||||
quantity: z.coerce.string().or(z.number()).optional().nullable(),
|
supplyType: z.string().optional(),
|
||||||
unit: z.string().min(1, { message: 'Unidad es requerida' }).nullable(),
|
quantity: z.coerce.string().or(z.number()).optional(), // Aceptamos string o number por los inputs
|
||||||
});
|
});
|
||||||
|
|
||||||
const equipmentItemSchema = z.object({
|
const equipmentItemSchema = z.object({
|
||||||
machine: z.string().nullable(),
|
machine: z.string(),
|
||||||
quantity: z.coerce.string().or(z.number()).optional().nullable(),
|
specifications: z.string().optional(),
|
||||||
|
quantity: z.coerce.string().or(z.number()).optional(), // Aceptamos string o number por los inputs
|
||||||
});
|
});
|
||||||
|
|
||||||
export const trainingSchema = z.object({
|
export const trainingSchema = z.object({
|
||||||
//Datos de la visita
|
//Datos de la visita
|
||||||
id: z.number().optional(),
|
id: z.number().optional(),
|
||||||
coorFullName: z
|
firstname: z.string().min(1, { message: 'Nombre es requerido' }),
|
||||||
.string()
|
lastname: z.string().min(1, { message: 'Apellido es requerido' }),
|
||||||
.min(1, { message: 'Nombre del coordinador es requerido' }),
|
coorPhone: z.string().optional().nullable(),
|
||||||
coorPhone: z.string().refine((val) => /^(04|02)\d{9}$/.test(val), {
|
|
||||||
message: 'El teléfono debe tener 11 dígitos y comenzar con 04 o 02',
|
|
||||||
}),
|
|
||||||
visitDate: z
|
visitDate: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Fecha y hora de visita es requerida' }),
|
.min(1, { message: 'Fecha y hora de visita es requerida' }),
|
||||||
|
|
||||||
//Datos de la organización socioproductiva (OSP)
|
//Datos de la organización socioproductiva (OSP)
|
||||||
ospType: z.string().min(1, { message: 'Tipo de OSP es requerido' }),
|
ospType: z.string().min(1, { message: 'Tipo de OSP es requerido' }),
|
||||||
ecoSector: z.string({ message: 'Sector Económico es requerido' }),
|
ecoSector: z.string().optional().or(z.literal('')),
|
||||||
productiveSector: z.string({ message: 'Sector Productivo es requerido' }),
|
productiveSector: z.string().optional().or(z.literal('')),
|
||||||
centralProductiveActivity: z.string({
|
centralProductiveActivity: z.string().optional().or(z.literal('')),
|
||||||
message: 'Actividad Central Productiva es requerido',
|
mainProductiveActivity: z.string().optional().or(z.literal('')),
|
||||||
}),
|
productiveActivity: z
|
||||||
mainProductiveActivity: z.string({
|
.string()
|
||||||
message: 'Actividad Productiva Principal es requerida',
|
.min(1, { message: 'Actividad productiva es requerida' }),
|
||||||
}),
|
ospRif: z.string().optional().or(z.literal('')),
|
||||||
productiveActivity: z.string({
|
ospName: z.string().min(1, { message: 'Nombre de la OSP es requerido' }),
|
||||||
message: 'Actividad Productiva es requerida',
|
|
||||||
}),
|
|
||||||
productiveActivityOther: z.string().min(1, { message: 'Este campo es requerido' }).optional(),
|
|
||||||
ospRif: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
ospName: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
companyConstitutionYear: z.coerce
|
companyConstitutionYear: z.coerce
|
||||||
.number()
|
.number()
|
||||||
.min(1900, { message: 'Año inválido' })
|
.min(1900, { message: 'Año inválido' }),
|
||||||
.nullable(),
|
|
||||||
currentStatus: z
|
currentStatus: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Estatus actual es requerido' })
|
.min(1, { message: 'Estatus actual es requerido' })
|
||||||
.default('ACTIVA'),
|
.default('ACTIVA'),
|
||||||
infrastructureMt2: z.string({ message: 'Infraestructura es requerida' }),
|
infrastructureMt2: z.string().optional().or(z.literal('')),
|
||||||
hasTransport: z
|
hasTransport: z
|
||||||
.preprocess(
|
.preprocess((val) => val === 'true' || val === true, z.boolean())
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
.optional(),
|
||||||
z.boolean(),
|
structureType: z.string().optional().or(z.literal('')),
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.nullable()
|
|
||||||
.default(false),
|
|
||||||
structureType: z.string({ message: 'Tipo de estructura es requerido' }),
|
|
||||||
isOpenSpace: z
|
isOpenSpace: z
|
||||||
.preprocess(
|
.preprocess((val) => val === 'true' || val === true, z.boolean())
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
.optional(),
|
||||||
z.boolean(),
|
paralysisReason: z.string().optional().default(''),
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.nullable()
|
|
||||||
.default(false),
|
|
||||||
paralysisReason: z.string().optional().nullable(),
|
|
||||||
|
|
||||||
//Datos del Equipamiento
|
//Datos del Equipamiento
|
||||||
equipmentList: z.array(equipmentItemSchema).optional().default([]),
|
equipmentList: z.array(equipmentItemSchema).optional().default([]),
|
||||||
@@ -86,84 +89,37 @@ export const trainingSchema = z.object({
|
|||||||
// Datos de Actividad Productiva
|
// Datos de Actividad Productiva
|
||||||
productList: z.array(productItemSchema).optional().default([]),
|
productList: z.array(productItemSchema).optional().default([]),
|
||||||
|
|
||||||
// Distribución y Exportación
|
|
||||||
internalDistributionZone: z
|
|
||||||
.string()
|
|
||||||
.min(1, { message: 'Zona de distribución es requerida' }),
|
|
||||||
isExporting: z
|
|
||||||
.preprocess(
|
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
|
||||||
z.boolean(),
|
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.default(false),
|
|
||||||
externalCountry: z.string().optional().nullable(),
|
|
||||||
externalCity: z.string().optional().nullable(),
|
|
||||||
externalDescription: z.string().optional().nullable(),
|
|
||||||
externalQuantity: z.coerce.string().or(z.number()).optional().nullable(),
|
|
||||||
externalUnit: z.string().optional().nullable(),
|
|
||||||
|
|
||||||
// Mano de obra
|
|
||||||
womenCount: z.coerce
|
|
||||||
.number()
|
|
||||||
.min(0, { message: 'Cantidad de mujeres es requerida' }),
|
|
||||||
menCount: z.coerce
|
|
||||||
.number()
|
|
||||||
.min(0, { message: 'Cantidad de hombres es requerida' }),
|
|
||||||
|
|
||||||
//Detalles de la ubicación
|
//Detalles de la ubicación
|
||||||
ospAddress: z
|
ospAddress: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Dirección de la OSP es requerida' }),
|
.min(1, { message: 'Dirección de la OSP es requerida' }),
|
||||||
ospGoogleMapsLink: z.string().optional().or(z.literal('')).nullable(),
|
ospGoogleMapsLink: z.string().optional().or(z.literal('')),
|
||||||
communeName: z
|
communeName: z.string().optional().or(z.literal('')),
|
||||||
.string()
|
siturCodeCommune: z.string().optional().or(z.literal('')),
|
||||||
.min(1, { message: 'Nombre de la comuna es requerida' }),
|
communeRif: z.string().optional().or(z.literal('')),
|
||||||
siturCodeCommune: z
|
communeSpokespersonName: z.string().optional().or(z.literal('')),
|
||||||
.string()
|
communeSpokespersonCedula: z.string().optional().or(z.literal('')),
|
||||||
.min(1, { message: 'Código SITUR de la comuna es requerida' }),
|
communeSpokespersonRif: z.string().optional().or(z.literal('')),
|
||||||
communeRif: z.string().min(1, { message: 'Rif de la comuna es requerida' }),
|
communeSpokespersonPhone: z.string().optional().or(z.literal('')),
|
||||||
communeSpokespersonName: z
|
|
||||||
.string()
|
|
||||||
.min(1, { message: 'Nombre del vocero de la comuna es requerido' }),
|
|
||||||
communeSpokespersonPhone: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.or(z.literal(''))
|
|
||||||
.refine((val) => !val || /^(04|02)\d{9}$/.test(val), {
|
|
||||||
message: 'El teléfono debe tener 11 dígitos y comenzar con 04 o 02',
|
|
||||||
}),
|
|
||||||
communeEmail: z
|
communeEmail: z
|
||||||
.string()
|
.string()
|
||||||
.email({ message: 'Correo electrónico de la Comuna inválido' })
|
.email({ message: 'Correo electrónico de la Comuna inválido' })
|
||||||
.optional()
|
.optional()
|
||||||
.or(z.literal(''))
|
.or(z.literal('')),
|
||||||
.nullable(),
|
|
||||||
communalCouncil: z
|
communalCouncil: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Consejo Comunal es requerido' }),
|
.min(1, { message: 'Consejo Comunal es requerido' }),
|
||||||
siturCodeCommunalCouncil: z
|
siturCodeCommunalCouncil: z.string().optional().or(z.literal('')),
|
||||||
.string()
|
communalCouncilRif: z.string().optional().or(z.literal('')),
|
||||||
.min(1, { message: 'Código SITUR del Consejo Comunal es requerido' }),
|
communalCouncilSpokespersonName: z.string().optional().or(z.literal('')),
|
||||||
communalCouncilRif: z
|
communalCouncilSpokespersonCedula: z.string().optional().or(z.literal('')),
|
||||||
.string()
|
communalCouncilSpokespersonRif: z.string().optional().or(z.literal('')),
|
||||||
.min(1, { message: 'Rif del Consejo Comunal es requerido' }),
|
communalCouncilSpokespersonPhone: z.string().optional().or(z.literal('')),
|
||||||
communalCouncilSpokespersonName: z
|
|
||||||
.string()
|
|
||||||
.min(1, { message: 'Nombre del vocero es requerido' }),
|
|
||||||
communalCouncilSpokespersonPhone: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.or(z.literal(''))
|
|
||||||
.refine((val) => !val || /^(04|02)\d{9}$/.test(val), {
|
|
||||||
message: 'El teléfono debe tener 11 dígitos y comenzar con 04 o 02',
|
|
||||||
}),
|
|
||||||
communalCouncilEmail: z
|
communalCouncilEmail: z
|
||||||
.string()
|
.string()
|
||||||
.email({ message: 'Correo electrónico del Consejo Comunal inválido' })
|
.email({ message: 'Correo electrónico del Consejo Comunal inválido' })
|
||||||
.optional()
|
.optional()
|
||||||
.or(z.literal(''))
|
.or(z.literal('')),
|
||||||
.nullable(),
|
|
||||||
|
|
||||||
//Datos del Responsable OSP
|
//Datos del Responsable OSP
|
||||||
ospResponsibleCedula: z
|
ospResponsibleCedula: z
|
||||||
@@ -172,153 +128,46 @@ export const trainingSchema = z.object({
|
|||||||
ospResponsibleFullname: z
|
ospResponsibleFullname: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Nombre del responsable es requerido' }),
|
.min(1, { message: 'Nombre del responsable es requerido' }),
|
||||||
ospResponsibleRif: z.string().optional().nullable(),
|
ospResponsibleRif: z
|
||||||
civilState: z.string().optional().nullable(),
|
.string()
|
||||||
|
.min(1, { message: 'RIF del responsable es requerido' }),
|
||||||
|
civilState: z.string().min(1, { message: 'Estado civil es requerido' }),
|
||||||
ospResponsiblePhone: z
|
ospResponsiblePhone: z
|
||||||
.string()
|
.string()
|
||||||
.min(1, { message: 'Teléfono del responsable es requerido' })
|
.min(1, { message: 'Teléfono del responsable es requerido' }),
|
||||||
.regex(/^(04|02)\d{9}$/, {
|
|
||||||
message: 'El teléfono debe tener 11 dígitos y comenzar con 04 o 02',
|
|
||||||
}),
|
|
||||||
ospResponsibleEmail: z
|
ospResponsibleEmail: z
|
||||||
.string()
|
.string()
|
||||||
.email({ message: 'Correo electrónico inválido' })
|
.email({ message: 'Correo electrónico inválido' }),
|
||||||
.optional()
|
familyBurden: z.coerce
|
||||||
.or(z.literal(''))
|
.number()
|
||||||
.nullable(),
|
.min(0, { message: 'Carga familiar requerida' }),
|
||||||
|
numberOfChildren: z.coerce
|
||||||
familyBurden: z.coerce.number().optional(),
|
.number()
|
||||||
numberOfChildren: z.coerce.number().optional(),
|
.min(0, { message: 'Número de hijos requerido' }),
|
||||||
|
|
||||||
//Datos adicionales
|
//Datos adicionales
|
||||||
generalObservations: z.string().optional().nullable(),
|
generalObservations: z.string().optional().default(''),
|
||||||
|
|
||||||
//IMAGENES
|
//IMAGENES
|
||||||
files: z.any().optional(),
|
files: z.any().optional(),
|
||||||
|
|
||||||
//no se envia la backend al crear ni editar el formulario
|
//no se envia la backend al crear ni editar el formulario
|
||||||
state: z.number({ message: 'El estado es requerido' }).nullable(),
|
state: z.number().optional().nullable(),
|
||||||
municipality: z.number({ message: 'Municipio es requerido' }).nullable(),
|
municipality: z.number().optional().nullable(),
|
||||||
parish: z.number({ message: 'Parroquia es requerido' }).nullable(),
|
parish: z.number().optional().nullable(),
|
||||||
coorState: z.number().optional().nullable(),
|
coorState: z.number().optional().nullable(),
|
||||||
coorMunicipality: z.number().optional().nullable(),
|
coorMunicipality: z.number().optional().nullable(),
|
||||||
coorParish: z.number().optional().nullable(),
|
coorParish: z.number().optional().nullable(),
|
||||||
photo1: z.string().optional().nullable(),
|
photo1: z.string().optional().nullable(),
|
||||||
photo2: z.string().optional().nullable(),
|
photo2: z.string().optional().nullable(),
|
||||||
photo3: z.string().optional().nullable(),
|
photo3: z.string().optional().nullable(),
|
||||||
createdBy: z.number().optional().nullable(),
|
|
||||||
updatedBy: z.number().optional().nullable(),
|
|
||||||
created_at: z.string().optional().nullable(),
|
|
||||||
updated_at: z.string().optional().nullable(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export type TrainingSchema = z.infer<typeof trainingSchema>;
|
export type TrainingSchema = z.infer<typeof trainingSchema>;
|
||||||
|
|
||||||
export const getTrainingSchema = z.object({
|
|
||||||
//Datos de la visita
|
|
||||||
id: z.number().optional(),
|
|
||||||
coorFullName: z.string(),
|
|
||||||
coorPhone: z.string(),
|
|
||||||
visitDate: z.string(),
|
|
||||||
//Datos de la organización socioproductiva (OSP)
|
|
||||||
ospType: z.string(),
|
|
||||||
ecoSector: z.string(),
|
|
||||||
productiveSector: z.string(),
|
|
||||||
centralProductiveActivity: z.string(),
|
|
||||||
mainProductiveActivity: z.string(),
|
|
||||||
productiveActivity: z.string(),
|
|
||||||
productiveActivityOther: z.string(),
|
|
||||||
ospRif: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
ospName: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
companyConstitutionYear: z.coerce.number(),
|
|
||||||
currentStatus: z.string(),
|
|
||||||
infrastructureMt2: z.string(),
|
|
||||||
hasTransport: z
|
|
||||||
.preprocess(
|
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
|
||||||
z.boolean(),
|
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.nullable()
|
|
||||||
.default(false),
|
|
||||||
structureType: z.string(),
|
|
||||||
isOpenSpace: z
|
|
||||||
.preprocess(
|
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
|
||||||
z.boolean(),
|
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.nullable()
|
|
||||||
.default(false),
|
|
||||||
paralysisReason: z.string().optional().nullable(),
|
|
||||||
//Datos del Equipamiento
|
|
||||||
equipmentList: z.array(equipmentItemSchema).optional().default([]),
|
|
||||||
//Datos de Producción
|
|
||||||
productionList: z.array(productionItemSchema).optional().default([]),
|
|
||||||
// Datos de Actividad Productiva
|
|
||||||
productList: z.array(productItemSchema).optional().default([]),
|
|
||||||
// Distribución y Exportación
|
|
||||||
internalDistributionZone: z.string(),
|
|
||||||
isExporting: z
|
|
||||||
.preprocess(
|
|
||||||
(val) => val === 'true' || val === true || val === 1 || val === '1',
|
|
||||||
z.boolean(),
|
|
||||||
)
|
|
||||||
.optional()
|
|
||||||
.default(false),
|
|
||||||
externalCountry: z.string().optional().nullable(),
|
|
||||||
externalCity: z.string().optional().nullable(),
|
|
||||||
externalDescription: z.string().optional().nullable(),
|
|
||||||
externalQuantity: z.coerce.string().or(z.number()).optional().nullable(),
|
|
||||||
externalUnit: z.string().optional().nullable(),
|
|
||||||
// Mano de obra
|
|
||||||
womenCount: z.coerce.number(),
|
|
||||||
menCount: z.coerce.number(),
|
|
||||||
//Detalles de la ubicación
|
|
||||||
ospAddress: z.string(),
|
|
||||||
ospGoogleMapsLink: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
communeName: z.string(),
|
|
||||||
siturCodeCommune: z.string(),
|
|
||||||
communeRif: z.string().or(z.literal('')).nullable(),
|
|
||||||
communeSpokespersonName: z.string().or(z.literal('')).nullable(),
|
|
||||||
communeSpokespersonPhone: z.string(),
|
|
||||||
communeEmail: z.string().optional().or(z.literal('')).nullable(),
|
|
||||||
communalCouncil: z.string(),
|
|
||||||
siturCodeCommunalCouncil: z.string(),
|
|
||||||
communalCouncilRif: z.string().optional(),
|
|
||||||
communalCouncilSpokespersonName: z.string(),
|
|
||||||
communalCouncilSpokespersonPhone: z.string(),
|
|
||||||
communalCouncilEmail: z.string(),
|
|
||||||
//Datos del Responsable OSP
|
|
||||||
ospResponsibleCedula: z.string(),
|
|
||||||
ospResponsibleFullname: z.string(),
|
|
||||||
ospResponsibleRif: z.string().optional().nullable(),
|
|
||||||
civilState: z.string().optional().nullable(),
|
|
||||||
ospResponsiblePhone: z.string(),
|
|
||||||
ospResponsibleEmail: z.string(),
|
|
||||||
familyBurden: z.coerce.number().optional(),
|
|
||||||
numberOfChildren: z.coerce.number().optional(),
|
|
||||||
//Datos adicionales
|
|
||||||
generalObservations: z.string().optional().nullable(),
|
|
||||||
//no se envia la backend al crear ni editar el formulario
|
|
||||||
state: z.number().nullable(),
|
|
||||||
municipality: z.number().nullable(),
|
|
||||||
parish: z.number().nullable(),
|
|
||||||
coorState: z.number().optional().nullable(),
|
|
||||||
coorMunicipality: z.number().optional().nullable(),
|
|
||||||
coorParish: z.number().optional().nullable(),
|
|
||||||
photo1: z.string().optional().nullable(),
|
|
||||||
photo2: z.string().optional().nullable(),
|
|
||||||
photo3: z.string().optional().nullable(),
|
|
||||||
createdBy: z.number().optional().nullable(),
|
|
||||||
updatedBy: z.number().optional().nullable(),
|
|
||||||
created_at: z.string().optional().nullable(),
|
|
||||||
updated_at: z.string().optional().nullable(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const trainingApiResponseSchema = z.object({
|
export const trainingApiResponseSchema = z.object({
|
||||||
message: z.string(),
|
message: z.string(),
|
||||||
data: z.array(getTrainingSchema),
|
data: z.array(trainingSchema),
|
||||||
meta: z.object({
|
meta: z.object({
|
||||||
page: z.number(),
|
page: z.number(),
|
||||||
limit: z.number(),
|
limit: z.number(),
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
|
||||||
import { auth } from '@/lib/auth';
|
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
import { cache } from 'react';
|
|
||||||
|
|
||||||
export const getValidAccessToken = cache(async () => {
|
|
||||||
const session = await auth();
|
|
||||||
|
|
||||||
if (!session?.access_token) {
|
|
||||||
// console.log('No hay Access Token');
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
// console.log('Si hay Access Token');
|
|
||||||
|
|
||||||
|
|
||||||
const now = Math.floor(Date.now() / 1000);
|
|
||||||
// Restamos 10s para tener margen de seguridad
|
|
||||||
const isValid = (session.access_expire_in as number) - 10 > now;
|
|
||||||
|
|
||||||
// A. Si es válido, lo retornamos directo
|
|
||||||
if (isValid) return session.access_token;
|
|
||||||
// console.log('Access Token Expiró');
|
|
||||||
|
|
||||||
// B. Si expiró, buscamos la cookie
|
|
||||||
const cookieStore = cookies();
|
|
||||||
const cookie = await cookieStore
|
|
||||||
const refreshToken = cookie.get('refresh_token')?.value;
|
|
||||||
const teaToken = cookie.get('tea_token')?.value;
|
|
||||||
|
|
||||||
if (!refreshToken) {
|
|
||||||
// console.log('No hay Refresh Token');
|
|
||||||
// Si no hay refres pero si access token pero ya expiro borrar la cookie para forzar cierre de session
|
|
||||||
(await cookieStore).delete('authjs.session-token');// comentar si por algun motivo da error
|
|
||||||
return null
|
|
||||||
} // No hay refresh token, fin del juego
|
|
||||||
// console.log('Si hay Refresh Token');
|
|
||||||
|
|
||||||
if (teaToken) {
|
|
||||||
return teaToken
|
|
||||||
}
|
|
||||||
|
|
||||||
// C. Intentamos refrescar
|
|
||||||
const newTokens = await resfreshTokenAction({ refreshToken });
|
|
||||||
|
|
||||||
if (!newTokens) {
|
|
||||||
// console.log('No hay token nuevo');
|
|
||||||
// Si falla el refresh (token revocado o expirado), borramos cookies
|
|
||||||
(await cookieStore).delete('refresh_token');
|
|
||||||
(await cookieStore).delete('authjs.session-token');// comentar si por algun motivo da error
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// console.log('Si hay token nuevo');
|
|
||||||
|
|
||||||
// console.log('Guardamos refresh');
|
|
||||||
// D. Guardamos el nuevo refresh token en cookie y retornamos el access token
|
|
||||||
(await cookieStore).set('refresh_token', newTokens.refresh_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
sameSite: 'lax',
|
|
||||||
path: '/',
|
|
||||||
maxAge: 7 * 24 * 60 * 60,
|
|
||||||
});
|
|
||||||
|
|
||||||
// console.log('guardamo tea');
|
|
||||||
(await cookieStore).set('tea_token', newTokens.access_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
sameSite: 'lax',
|
|
||||||
path: '/',
|
|
||||||
maxAge: 7 * 24 * 60 * 60,
|
|
||||||
});
|
|
||||||
|
|
||||||
return newTokens.access_token;
|
|
||||||
});
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
// lib/auth.config.ts
|
|
||||||
import { SignInAction } from '@/feactures/auth/actions/login-action';
|
import { SignInAction } from '@/feactures/auth/actions/login-action';
|
||||||
import { logoutAction } from '@/feactures/auth/actions/logout-action';
|
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
||||||
import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
||||||
import { DefaultJWT, JWT } from 'next-auth/jwt';
|
// import { DefaultJWT } from 'next-auth/jwt';
|
||||||
import CredentialProvider from 'next-auth/providers/credentials';
|
import CredentialProvider from 'next-auth/providers/credentials';
|
||||||
|
|
||||||
|
|
||||||
// Define los tipos para tus respuestas de SignInAction
|
// Define los tipos para tus respuestas de SignInAction
|
||||||
interface SignInSuccessResponse {
|
interface SignInSuccessResponse {
|
||||||
message: string;
|
message: string;
|
||||||
@@ -57,10 +57,8 @@ const authConfig: NextAuthConfig = {
|
|||||||
|
|
||||||
// **NUEVO: Manejar el caso `null` primero**
|
// **NUEVO: Manejar el caso `null` primero**
|
||||||
if (response === null) {
|
if (response === null) {
|
||||||
console.error(
|
console.error("SignInAction returned null, indicating a potential issue before API call or generic error.");
|
||||||
'SignInAction returned null, indicating a potential issue before API call or generic error.',
|
throw new CredentialsSignin("Error de inicio de sesión inesperado.");
|
||||||
);
|
|
||||||
throw new CredentialsSignin('Error de inicio de sesión inesperado.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tipo Guarda: Verificar la respuesta de error
|
// Tipo Guarda: Verificar la respuesta de error
|
||||||
@@ -71,19 +69,15 @@ const authConfig: NextAuthConfig = {
|
|||||||
response.type === 'UNKNOWN_ERROR') // Incluye todos los tipos de error posibles
|
response.type === 'UNKNOWN_ERROR') // Incluye todos los tipos de error posibles
|
||||||
) {
|
) {
|
||||||
// Si es un error, lánzalo. Este camino termina aquí.
|
// Si es un error, lánzalo. Este camino termina aquí.
|
||||||
throw new CredentialsSignin('Error en la API:' + response.message);
|
throw new CredentialsSignin("Error en la API:" + response.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!('user' in response)) {
|
if (!('user' in response)) {
|
||||||
// Esto solo ocurriría si SignInAction devolvió un objeto que no es null,
|
// Esto solo ocurriría si SignInAction devolvió un objeto que no es null,
|
||||||
// no es un error conocido por 'type', PERO tampoco tiene la propiedad 'user'.
|
// no es un error conocido por 'type', PERO tampoco tiene la propiedad 'user'.
|
||||||
// Es un caso de respuesta inesperada del API.
|
// Es un caso de respuesta inesperada del API.
|
||||||
console.error(
|
console.error("Respuesta de SignInAction con formato inesperado: falta la propiedad 'user'.");
|
||||||
"Respuesta de SignInAction con formato inesperado: falta la propiedad 'user'.",
|
throw new CredentialsSignin("Error en el formato de la respuesta del servidor.");
|
||||||
);
|
|
||||||
throw new CredentialsSignin(
|
|
||||||
'Error en el formato de la respuesta del servidor.',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -94,6 +88,8 @@ const authConfig: NextAuthConfig = {
|
|||||||
role: response?.user.rol ?? [], // Add role array
|
role: response?.user.rol ?? [], // Add role array
|
||||||
access_token: response?.tokens.access_token ?? '',
|
access_token: response?.tokens.access_token ?? '',
|
||||||
access_expire_in: response?.tokens.access_expire_in ?? 0,
|
access_expire_in: response?.tokens.access_expire_in ?? 0,
|
||||||
|
refresh_token: response?.tokens.refresh_token ?? '',
|
||||||
|
refresh_expire_in: response?.tokens.refresh_expire_in ?? 0,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -102,7 +98,7 @@ const authConfig: NextAuthConfig = {
|
|||||||
signIn: '/', //sigin page
|
signIn: '/', //sigin page
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async jwt({ token, user }: { user: User; token: any }) {
|
async jwt({ token, user }: { user: User, token: any }) {
|
||||||
// 1. Manejar el inicio de sesión inicial
|
// 1. Manejar el inicio de sesión inicial
|
||||||
// El `user` solo se proporciona en el primer inicio de sesión.
|
// El `user` solo se proporciona en el primer inicio de sesión.
|
||||||
if (user) {
|
if (user) {
|
||||||
@@ -114,14 +110,54 @@ const authConfig: NextAuthConfig = {
|
|||||||
role: user.role,
|
role: user.role,
|
||||||
access_token: user.access_token,
|
access_token: user.access_token,
|
||||||
access_expire_in: user.access_expire_in,
|
access_expire_in: user.access_expire_in,
|
||||||
};
|
refresh_token: user.refresh_token,
|
||||||
|
refresh_expire_in: user.refresh_expire_in
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return token;
|
// 2. Si no es un nuevo login, verificar la expiración del token
|
||||||
|
const now = Math.floor(Date.now() / 1000); // Usar Math.floor para un número entero
|
||||||
|
|
||||||
|
// Verificar si el token de acceso aún es válido
|
||||||
|
if (now < (token.access_expire_in as number)) {
|
||||||
|
return token; // Si no ha expirado, no hacer nada y devolver el token actual
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Si el token de acceso ha expirado, verificar el refresh token
|
||||||
|
if (now > (token.refresh_expire_in as number)) {
|
||||||
|
return null; // Forzar el logout al devolver null
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Si el token de acceso ha expirado pero el refresh token es válido, renovar
|
||||||
|
console.log("Renovando token de acceso...");
|
||||||
|
try {
|
||||||
|
const refresh_token = { token: token.refresh_token as string, user_id: Number(token.id) as number }
|
||||||
|
|
||||||
|
const res = await resfreshTokenAction(refresh_token);
|
||||||
|
|
||||||
|
if (!res || !res.tokens) {
|
||||||
|
throw new Error('Fallo en la respuesta de la API de refresco.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualizar el token directamente con los nuevos valores
|
||||||
|
token.access_token = res.tokens.access_token;
|
||||||
|
token.access_expire_in = res.tokens.access_expire_in;
|
||||||
|
token.refresh_token = res.tokens.refresh_token;
|
||||||
|
token.refresh_expire_in = res.tokens.refresh_expire_in;
|
||||||
|
|
||||||
|
console.log("Token renovado exitosamente.");
|
||||||
|
return token;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null; // Fallo al renovar, forzar logout
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async session({ session, token }: { session: Session; token: DefaultJWT }) {
|
async session({ session, token }: { session: Session; token: any }) {
|
||||||
session.access_token = token.access_token as string;
|
session.access_token = token.access_token as string;
|
||||||
session.access_expire_in = token.access_expire_in as number;
|
session.access_expire_in = token.access_expire_in as number;
|
||||||
|
session.refresh_token = token.refresh_token as string;
|
||||||
|
session.refresh_expire_in = token.refresh_expire_in as number;
|
||||||
session.user = {
|
session.user = {
|
||||||
id: token.id as number,
|
id: token.id as number,
|
||||||
username: token.username as string,
|
username: token.username as string,
|
||||||
@@ -129,21 +165,11 @@ const authConfig: NextAuthConfig = {
|
|||||||
email: token.email as string,
|
email: token.email as string,
|
||||||
role: token.role as Array<{ id: number; rol: string }>,
|
role: token.role as Array<{ id: number; rol: string }>,
|
||||||
};
|
};
|
||||||
|
console.log("Session: Habilitado");
|
||||||
return session;
|
return session;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
events: {
|
|
||||||
async signOut(message) {
|
|
||||||
// 1. verificamos que venga token (puede no venir con algunos providers)
|
|
||||||
const token = (message as { token?: JWT }).token;
|
|
||||||
if (!token?.access_token) return;
|
|
||||||
try {
|
|
||||||
await logoutAction(String(token?.id));
|
|
||||||
} catch {
|
|
||||||
/* silencioso para que next-auth siempre cierre */
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} satisfies NextAuthConfig;
|
} satisfies NextAuthConfig;
|
||||||
|
|
||||||
export default authConfig;
|
export default authConfig;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use server';
|
'use server';
|
||||||
import { env } from '@/lib/env';
|
import { env } from '@/lib/env';
|
||||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
import axios from 'axios';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
// Crear instancia de Axios con la URL base validada
|
// Crear instancia de Axios con la URL base validada
|
||||||
@@ -10,21 +10,33 @@ const fetchApi = axios.create({
|
|||||||
|
|
||||||
// Interceptor para incluir el token automáticamente en las peticiones
|
// Interceptor para incluir el token automáticamente en las peticiones
|
||||||
// ESTE INTERCEPTOR ESTÁ BIEN PARA EL RESTO DE LAS PETICIONES AUTENTICADAS
|
// ESTE INTERCEPTOR ESTÁ BIEN PARA EL RESTO DE LAS PETICIONES AUTENTICADAS
|
||||||
fetchApi.interceptors.request.use(
|
fetchApi.interceptors.request.use(async (config: any) => {
|
||||||
async (config: InternalAxiosRequestConfig) => {
|
try {
|
||||||
try {
|
// console.log("Solicitando autenticación...");
|
||||||
const { getValidAccessToken } = await import('@/lib/auth-token');
|
|
||||||
const token = await getValidAccessToken();
|
const { auth } = await import('@/lib/auth'); // Importación dinámica
|
||||||
|
const session = await auth();
|
||||||
|
const token = session?.access_token;
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
config.headers.set('Authorization', `Bearer ${token}`);
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error getting auth token:', err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// **Importante:** Si el body es FormData, elimina el Content-Type para que Axios lo configure automáticamente.
|
||||||
|
if (config.data instanceof FormData) {
|
||||||
|
delete config.headers['Content-Type'];
|
||||||
|
} else {
|
||||||
|
config.headers['Content-Type'] = 'application/json';
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
},
|
} catch (error) {
|
||||||
);
|
console.error('Error al obtener el token de autenticación para el interceptor:', error);
|
||||||
|
// IMPORTANTE: Si ocurre un error aquí, es mejor rechazar la promesa
|
||||||
|
// para que la solicitud no se envíe sin autorización.
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// safeFetchApi sigue siendo útil para el resto de las llamadas que requieren autenticación
|
// safeFetchApi sigue siendo útil para el resto de las llamadas que requieren autenticación
|
||||||
export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||||
@@ -85,4 +97,4 @@ export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { fetchApi };
|
export { fetchApi };
|
||||||
99
apps/web/lib/fetch.api2.ts
Normal file
99
apps/web/lib/fetch.api2.ts
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
'use server';
|
||||||
|
import { env } from '@/lib/env'; // Importamos la configuración de entorno validada
|
||||||
|
import axios from 'axios';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
// Crear instancia de Axios con la URL base validada
|
||||||
|
const fetchApi = axios.create({
|
||||||
|
baseURL: env.API_URL, // Aquí usamos env.API_URL en vez de process.env.BACKEND_URL
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Interceptor para incluir el token automáticamente en las peticiones
|
||||||
|
fetchApi.interceptors.request.use(async (config: any) => {
|
||||||
|
try {
|
||||||
|
// Importación dinámica para evitar la referencia circular
|
||||||
|
const { auth } = await import('@/lib/auth');
|
||||||
|
const session = await auth();
|
||||||
|
const token = session?.access_token;
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting auth token:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Función para hacer peticiones con validación de respuesta
|
||||||
|
* @param schema - Esquema de Zod para validar la respuesta
|
||||||
|
* @param url - Endpoint a consultar
|
||||||
|
* @param config - Configuración opcional de Axios
|
||||||
|
* @returns [error, data] -> Devuelve el error como objeto estructurado si hay fallo, o los datos validados
|
||||||
|
*/
|
||||||
|
export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||||
|
schema: T,
|
||||||
|
url: string,
|
||||||
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'GET',
|
||||||
|
body?: any,
|
||||||
|
): Promise<
|
||||||
|
[{ type: string; message: string; details?: any } | null, z.infer<T> | null]
|
||||||
|
> => {
|
||||||
|
try {
|
||||||
|
const response = await fetchApi({
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
data: body,
|
||||||
|
});
|
||||||
|
|
||||||
|
const parsed = schema.safeParse(response.data);
|
||||||
|
|
||||||
|
if (!parsed.success) {
|
||||||
|
console.error('Validation Error Details:', {
|
||||||
|
errors: parsed.error.errors,
|
||||||
|
receivedData: response.data,
|
||||||
|
expectedSchema: schema,
|
||||||
|
data: response.data.data,
|
||||||
|
});
|
||||||
|
// console.error(parsed.error.errors)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
type: 'VALIDATION_ERROR',
|
||||||
|
message: 'Validation error',
|
||||||
|
details: parsed.error.errors,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [null, parsed.data];
|
||||||
|
} catch (error: any) {
|
||||||
|
const errorDetails = {
|
||||||
|
status: error.response?.status,
|
||||||
|
statusText: error.response?.statusText,
|
||||||
|
message: error.message,
|
||||||
|
url: error.config?.url,
|
||||||
|
method: error.config?.method,
|
||||||
|
requestData: error.config?.data,
|
||||||
|
responseData: error.response?.data,
|
||||||
|
headers: error.config?.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
// console.log(error)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
type: 'API_ERROR',
|
||||||
|
message: error.response?.data?.message || 'Unknown API error',
|
||||||
|
details: errorDetails,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { fetchApi };
|
||||||
6
apps/web/types/next-auth.d.ts
vendored
6
apps/web/types/next-auth.d.ts
vendored
@@ -4,6 +4,8 @@ declare module 'next-auth' {
|
|||||||
interface Session extends DefaultSession {
|
interface Session extends DefaultSession {
|
||||||
access_token: string;
|
access_token: string;
|
||||||
access_expire_in: number;
|
access_expire_in: number;
|
||||||
|
refresh_token: string;
|
||||||
|
refresh_expire_in: number;
|
||||||
user: {
|
user: {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
@@ -27,6 +29,8 @@ declare module 'next-auth' {
|
|||||||
}>;
|
}>;
|
||||||
access_token: string;
|
access_token: string;
|
||||||
access_expire_in: number;
|
access_expire_in: number;
|
||||||
|
refresh_token: string;
|
||||||
|
refresh_expire_in: number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,5 +46,7 @@ declare module 'next-auth/jwt' {
|
|||||||
}>;
|
}>;
|
||||||
access_token: string;
|
access_token: string;
|
||||||
access_expire_in: number;
|
access_expire_in: number;
|
||||||
|
refresh_token: string;
|
||||||
|
refresh_expire_in: number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,38 +8,38 @@
|
|||||||
@custom-variant dark (&:is(.dark *));
|
@custom-variant dark (&:is(.dark *));
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
--background: hsl(51, 76%, 97%);
|
--background: hsl(0 0% 100%);
|
||||||
--foreground: hsl(240 10% 3.9%);
|
--foreground: hsl(240 10% 3.9%);
|
||||||
--card: hsl(0 0% 100%);
|
--card: hsl(0 0% 100%);
|
||||||
--card-foreground: hsl(240 10% 3.9%);
|
--card-foreground: hsl(240 10% 3.9%);
|
||||||
--popover: hsl(0 0% 100%);
|
--popover: hsl(0 0% 100%);
|
||||||
--popover-foreground: hsl(240 10% 3.9%);
|
--popover-foreground: hsl(240 10% 3.9%);
|
||||||
--primary: hsl(17, 86%, 45%);
|
--primary: hsl(3, 85%, 32%);
|
||||||
--primary-foreground: hsl(355.7 100% 97.3%);
|
--primary-foreground: hsl(355.7 100% 97.3%);
|
||||||
--secondary: hsl(240 4.8% 95.9%);
|
--secondary: hsl(240 4.8% 95.9%);
|
||||||
--secondary-foreground: hsl(240 5.9% 10%);
|
--secondary-foreground: hsl(240 5.9% 10%);
|
||||||
--muted: hsl(240 4.8% 95.9%);
|
--muted: hsl(240 4.8% 95.9%);
|
||||||
--muted-foreground: hsl(240, 2%, 31%);
|
--muted-foreground: hsl(240 3.8% 46.1%);
|
||||||
--accent: hsl(240 4.8% 95.9%);
|
--accent: hsl(240 4.8% 95.9%);
|
||||||
--accent-foreground: hsl(240 5.9% 10%);
|
--accent-foreground: hsl(240 5.9% 10%);
|
||||||
--destructive: hsl(0 84.2% 60.2%);
|
--destructive: hsl(0 84.2% 60.2%);
|
||||||
--destructive-foreground: hsl(0 0% 98%);
|
--destructive-foreground: hsl(0 0% 98%);
|
||||||
--border: hsl(240 5.9% 90%);
|
--border: hsl(240 5.9% 90%);
|
||||||
--input: hsl(240 5.9% 90%);
|
--input: hsl(240 5.9% 90%);
|
||||||
--ring: hsl(3, 85%, 32%);
|
--ring: hsl(3, 85%, 32%);
|
||||||
--radius: 0.7rem;
|
--radius: 0.7rem;
|
||||||
--chart-1: hsl(12 76% 61%);
|
--chart-1: hsl(12 76% 61%);
|
||||||
--chart-2: hsl(173 58% 39%);
|
--chart-2: hsl(173 58% 39%);
|
||||||
--chart-3: hsl(197 37% 24%);
|
--chart-3: hsl(197 37% 24%);
|
||||||
--chart-4: hsl(43 74% 66%);
|
--chart-4: hsl(43 74% 66%);
|
||||||
--chart-5: hsl(27 87% 67%);
|
--chart-5: hsl(27 87% 67%);
|
||||||
--sidebar-background: hsl(27, 92%, 90%);
|
--sidebar-background: hsl(0 0% 98%);
|
||||||
--sidebar-foreground: hsl(0, 0%, 1%);
|
--sidebar-foreground: hsl(240 5.3% 26.1%);
|
||||||
--sidebar-primary: hsl(240 5.9% 10%);
|
--sidebar-primary: hsl(240 5.9% 10%);
|
||||||
--sidebar-primary-foreground: hsl(0 0% 98%);
|
--sidebar-primary-foreground: hsl(0 0% 98%);
|
||||||
--sidebar-accent: hsl(24, 82%, 67%);
|
--sidebar-accent: hsl(240 4.8% 95.9%);
|
||||||
--sidebar-accent-foreground: hsl(240 5.9% 10%);
|
--sidebar-accent-foreground: hsl(240 5.9% 10%);
|
||||||
--sidebar-border: hsl(20, 13%, 91%);
|
--sidebar-border: hsl(220 13% 91%);
|
||||||
--sidebar-ring: hsl(217.2 91.2% 59.8%);
|
--sidebar-ring: hsl(217.2 91.2% 59.8%);
|
||||||
--sidebar: hsl(0 0% 98%);
|
--sidebar: hsl(0 0% 98%);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,184 +0,0 @@
|
|||||||
@import 'tailwindcss';
|
|
||||||
|
|
||||||
@source './components/ui';
|
|
||||||
|
|
||||||
@plugin "tailwindcss-animate";
|
|
||||||
@plugin "@tailwindcss/typography";
|
|
||||||
|
|
||||||
@custom-variant dark (&:is(.dark *));
|
|
||||||
|
|
||||||
:root {
|
|
||||||
--background: hsl(0 0% 100%);
|
|
||||||
--foreground: hsl(240 10% 3.9%);
|
|
||||||
--card: hsl(0 0% 100%);
|
|
||||||
--card-foreground: hsl(240 10% 3.9%);
|
|
||||||
--popover: hsl(0 0% 100%);
|
|
||||||
--popover-foreground: hsl(240 10% 3.9%);
|
|
||||||
--primary: hsl(3, 85%, 32%);
|
|
||||||
--primary-foreground: hsl(355.7 100% 97.3%);
|
|
||||||
--secondary: hsl(240 4.8% 95.9%);
|
|
||||||
--secondary-foreground: hsl(240 5.9% 10%);
|
|
||||||
--muted: hsl(240 4.8% 95.9%);
|
|
||||||
--muted-foreground: hsl(240 3.8% 46.1%);
|
|
||||||
--accent: hsl(240 4.8% 95.9%);
|
|
||||||
--accent-foreground: hsl(240 5.9% 10%);
|
|
||||||
--destructive: hsl(0 84.2% 60.2%);
|
|
||||||
--destructive-foreground: hsl(0 0% 98%);
|
|
||||||
--border: hsl(240 5.9% 90%);
|
|
||||||
--input: hsl(240 5.9% 90%);
|
|
||||||
--ring: hsl(3, 85%, 32%);
|
|
||||||
--radius: 0.7rem;
|
|
||||||
--chart-1: hsl(12 76% 61%);
|
|
||||||
--chart-2: hsl(173 58% 39%);
|
|
||||||
--chart-3: hsl(197 37% 24%);
|
|
||||||
--chart-4: hsl(43 74% 66%);
|
|
||||||
--chart-5: hsl(27 87% 67%);
|
|
||||||
--sidebar-background: hsl(0 0% 98%);
|
|
||||||
--sidebar-foreground: hsl(240 5.3% 26.1%);
|
|
||||||
--sidebar-primary: hsl(240 5.9% 10%);
|
|
||||||
--sidebar-primary-foreground: hsl(0 0% 98%);
|
|
||||||
--sidebar-accent: hsl(240 4.8% 95.9%);
|
|
||||||
--sidebar-accent-foreground: hsl(240 5.9% 10%);
|
|
||||||
--sidebar-border: hsl(220 13% 91%);
|
|
||||||
--sidebar-ring: hsl(217.2 91.2% 59.8%);
|
|
||||||
--sidebar: hsl(0 0% 98%);
|
|
||||||
}
|
|
||||||
|
|
||||||
.dark {
|
|
||||||
--background: hsl(20 14.3% 4.1%);
|
|
||||||
--foreground: hsl(0 0% 95%);
|
|
||||||
--card: hsl(240 5.9% 10%);
|
|
||||||
--card-foreground: hsl(0 0% 95%);
|
|
||||||
--popover: hsl(0 0% 9%);
|
|
||||||
--popover-foreground: hsl(0 0% 95%);
|
|
||||||
--primary: hsl(3, 85%, 32%);
|
|
||||||
--primary-foreground: hsl(180, 33%, 99%);
|
|
||||||
--secondary: hsl(240 3.7% 15.9%);
|
|
||||||
--secondary-foreground: hsl(0 0% 98%);
|
|
||||||
--muted: hsl(0 0% 15%);
|
|
||||||
--muted-foreground: hsl(240 5% 64.9%);
|
|
||||||
--accent: hsl(12 6.5% 15.1%);
|
|
||||||
--accent-foreground: hsl(0 0% 98%);
|
|
||||||
--destructive: hsl(0 62.8% 30.6%);
|
|
||||||
--destructive-foreground: hsl(0 85.7% 97.3%);
|
|
||||||
--border: hsl(240 3.7% 15.9%);
|
|
||||||
--input: hsl(240 3.7% 15.9%);
|
|
||||||
--ring: hsl(3, 85%, 32%);
|
|
||||||
/* --chart-1: hsl(220 70% 50%); */
|
|
||||||
--chart-1: hsl(22 70% 50%);
|
|
||||||
--chart-2: hsl(160 60% 45%);
|
|
||||||
--chart-3: hsl(30 80% 55%);
|
|
||||||
--chart-4: hsl(280 65% 60%);
|
|
||||||
--chart-5: hsl(340 75% 55%);
|
|
||||||
--sidebar-background: hsl(240 5.9% 10%);
|
|
||||||
--sidebar-foreground: hsl(240 4.8% 95.9%);
|
|
||||||
--sidebar-primary: hsl(224.3 76.3% 48%);
|
|
||||||
--sidebar-primary-foreground: hsl(0 0% 100%);
|
|
||||||
--sidebar-accent: hsl(240 3.7% 15.9%);
|
|
||||||
--sidebar-accent-foreground: hsl(240 4.8% 95.9%);
|
|
||||||
--sidebar-border: hsl(240 3.7% 15.9%);
|
|
||||||
--sidebar-ring: hsl(217.2 91.2% 59.8%);
|
|
||||||
--sidebar: hsl(240 5.9% 10%);
|
|
||||||
}
|
|
||||||
|
|
||||||
@theme inline {
|
|
||||||
--color-background: var(--background);
|
|
||||||
--color-foreground: var(--foreground);
|
|
||||||
--color-card: var(--card);
|
|
||||||
--color-card-foreground: var(--card-foreground);
|
|
||||||
--color-popover: var(--popover);
|
|
||||||
--color-popover-foreground: var(--popover-foreground);
|
|
||||||
--color-primary: var(--primary);
|
|
||||||
--color-primary-foreground: var(--primary-foreground);
|
|
||||||
--color-secondary: var(--secondary);
|
|
||||||
--color-secondary-foreground: var(--secondary-foreground);
|
|
||||||
--color-muted: var(--muted);
|
|
||||||
--color-muted-foreground: var(--muted-foreground);
|
|
||||||
--color-accent: var(--accent);
|
|
||||||
--color-accent-foreground: var(--accent-foreground);
|
|
||||||
--color-destructive: var(--destructive);
|
|
||||||
--color-destructive-foreground: var(--destructive-foreground);
|
|
||||||
--color-border: var(--border);
|
|
||||||
--color-input: var(--input);
|
|
||||||
--color-ring: var(--ring);
|
|
||||||
--color-chart-1: var(--chart-1);
|
|
||||||
--color-chart-2: var(--chart-2);
|
|
||||||
--color-chart-3: var(--chart-3);
|
|
||||||
--color-chart-4: var(--chart-4);
|
|
||||||
--color-chart-5: var(--chart-5);
|
|
||||||
--radius-sm: calc(var(--radius) - 4px);
|
|
||||||
--radius-md: calc(var(--radius) - 2px);
|
|
||||||
--radius-lg: var(--radius);
|
|
||||||
--radius-xl: calc(var(--radius) + 4px);
|
|
||||||
--color-sidebar-ring: var(--sidebar-ring);
|
|
||||||
--color-sidebar-border: var(--sidebar-border);
|
|
||||||
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
|
|
||||||
--color-sidebar-accent: var(--sidebar-accent);
|
|
||||||
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
|
|
||||||
--color-sidebar-primary: var(--sidebar-primary);
|
|
||||||
--color-sidebar-foreground: var(--sidebar-foreground);
|
|
||||||
--color-sidebar: var(--sidebar-background);
|
|
||||||
--animate-accordion-down: accordion-down 0.2s ease-out;
|
|
||||||
--animate-accordion-up: accordion-up 0.2s ease-out;
|
|
||||||
|
|
||||||
@keyframes accordion-down {
|
|
||||||
from {
|
|
||||||
height: 0;
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
height: var(--radix-accordion-content-height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes accordion-up {
|
|
||||||
from {
|
|
||||||
height: var(--radix-accordion-content-height);
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
height: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@layer base {
|
|
||||||
* {
|
|
||||||
@apply border-border outline-ring/50;
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
@apply bg-background text-foreground overscroll-none;
|
|
||||||
/* font-feature-settings: "rlig" 1, "calt" 1; */
|
|
||||||
font-synthesis-weight: none;
|
|
||||||
text-rendering: optimizeLegibility;
|
|
||||||
}
|
|
||||||
|
|
||||||
@supports (font: -apple-system-body) and (-webkit-appearance: none) {
|
|
||||||
[data-wrapper] {
|
|
||||||
@apply min-[1800px]:border-t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Custom scrollbar styling. Thanks @pranathiperii. */
|
|
||||||
::-webkit-scrollbar {
|
|
||||||
width: 5px;
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-track {
|
|
||||||
background: transparent;
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-thumb {
|
|
||||||
border-radius: 5px;
|
|
||||||
background: hsl(var(--border));
|
|
||||||
}
|
|
||||||
* {
|
|
||||||
scrollbar-color: hsl(var(--border)) transparent;
|
|
||||||
scrollbar-width: thin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@layer base {
|
|
||||||
* {
|
|
||||||
@apply border-border outline-ring/50;
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
@apply bg-background text-foreground;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user