FIX: map request body to the form wordpress

This commit is contained in:
marcos-silva-rodrigues
2024-12-18 13:58:08 -03:00
parent db2ea09d25
commit 9e2de28032
4 changed files with 79 additions and 20 deletions
-14
View File
@@ -1,14 +0,0 @@
import { ApiProperty } from "@nestjs/swagger";
export class CreateUserOpenDataDTO {
@ApiProperty()
firstName: string;
@ApiProperty()
lastName: string;
@ApiProperty()
email: string;
@ApiProperty()
organization: string;
@ApiProperty()
enquiryType: string;
}
@@ -0,0 +1,48 @@
import { ApiProperty } from "@nestjs/swagger";
export class CreateUserOpenDataDTO {
@ApiProperty()
firstName: string;
@ApiProperty()
lastName: string;
@ApiProperty()
email: string;
@ApiProperty()
organization: string;
@ApiProperty()
enquiryType: string;
}
type FormField = {
id: string;
type: string;
title: string;
value: string;
raw_value: string;
required: string;
};
type MetaData = {
title: string;
value: string;
};
export type WordpressForm = {
form: {
id: string;
name: string;
};
fields: {
[key: string]: FormField;
};
meta: {
date: MetaData;
time: MetaData;
page_url: MetaData;
user_agent: MetaData;
remote_ip: MetaData;
credit: MetaData;
};
};
+30 -5
View File
@@ -6,8 +6,8 @@ import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filt
import { LanguageEnum } from 'src/utils/languages.enum';
import { UsersService } from '../users/users.service';
import { Language } from 'src/decorators/language.decorator';
import { CreateUserOpenDataDTO } from './dto/create-user';
import { OpenDataService } from './open-data.service';
import { CreateUserOpenDataDTO, WordpressForm } from './dto/wordpres-form';
@Controller('open-data')
@ApiInternalOnlyController()
@@ -28,20 +28,45 @@ export class OpenDataController {
@Post("/sharing-ocean-data")
@ApiCreatedResponse()
async createUser(
@Body() body: CreateUserOpenDataDTO
@Body()
body: WordpressForm
) {
this.logger.info('createUser for open data' + JSON.stringify({ body }));
const OPENDATA_PUBLIC_USERS_GROUP_ID = "401573bb-334f-44b2-b30e-88d4cea31ae9";
const OPENDATA_CUSTOMER_ID = "f239718a-a271-4ef9-ae7e-02a2f0f3aa6e";
const roles = [OPENDATA_PUBLIC_USERS_GROUP_ID];
this.logger.info('createUser for open data' + JSON.stringify({ body }));
const data = {}
try {
await this.openDataService.createUser(OPENDATA_CUSTOMER_ID, body, roles);
Object.keys(body.fields)
.filter(key => body.fields[key].required === "1")
.forEach(key => {
const field = body.fields[key]
data[field.id] = field.value
});
} catch (e) {
this.logger.error('user data' + e.message);
}
const user: CreateUserOpenDataDTO = {
email: data["email"],
enquiryType: data["enquiry_type"],
firstName: data["first_name"],
lastName: data["last_name"],
organization: data["organization"]
}
this.logger.info('user data' + JSON.stringify({ user }));
try {
await this.openDataService.createUser(OPENDATA_CUSTOMER_ID, user, roles);
return "success"
} catch (e) {
this.logger.error('user data' + e.message);
return e;
}
// return ;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';
import { CreateUserOpenDataDTO } from './dto/create-user';
import { CreateUserOpenDataDTO } from './dto/wordpres-form';
import DadosferaLogger from '@dadosfera/dadosfera-logs';
import { UsersProtoService } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/write-service';
import { DucClient } from '../duc/client.config';