Skip to content

Commit

Permalink
Merge pull request #209 from tekdi/sprint6.0
Browse files Browse the repository at this point in the history
Issue #000 fix: merging changes from sprint-6.0 branch, implemented duriing june 15 pilot testing
  • Loading branch information
vaivk369 authored Jun 17, 2024
2 parents bca6c83 + 64fdf8f commit a71771d
Show file tree
Hide file tree
Showing 21 changed files with 444 additions and 317 deletions.
7 changes: 4 additions & 3 deletions src/adapters/fieldsservicelocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { FieldsDto } from "src/fields/dto/fields.dto";
import { FieldValuesDto } from "src/fields/dto/field-values.dto";
import { FieldValuesSearchDto } from "src/fields/dto/field-values-search.dto";
import { Response } from "express";

export interface IServicelocatorfields {
//fields
createFields(request: any, fieldsDto: FieldsDto, response: Response);
// getFields(tenantId, fieldsId, request);
searchFields(tenantid, request: any, fieldsSearchDto: FieldsSearchDto,response: Response);
searchFields(tenantid, request: any, fieldsSearchDto: FieldsSearchDto, response: Response);
// updateFields(fieldsId: string, request: any, fieldsDto: FieldsDto);
//field values
createFieldValues(request: any, fieldValuesDto: FieldValuesDto,response : Response);
createFieldValues(request: any, fieldValuesDto: FieldValuesDto, response: Response);
// getFieldValues(id, request);
searchFieldValues(request: any, fieldValuesSearchDto: FieldValuesSearchDto, response: Response);
updateFieldValues(id: string, request: any, fieldValuesDto: FieldValuesDto);
getFieldOptions(request: any, fieldName: string, controllingfieldfk: string, context: string, contextType: string, response: Response);

}
9 changes: 5 additions & 4 deletions src/adapters/hasura/fields.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import { FieldValuesSearchDto } from "src/fields/dto/field-values-search.dto";
import { IServicelocatorfields } from "../fieldsservicelocator";
import { UserDto } from "src/user/dto/user.dto";
export const HasuraFieldsToken = "HasuraFields";

import { FieldsService } from "./services/fields.service";

@Injectable()
export class HasuraFieldsService implements IServicelocatorfields {
constructor(
private httpService: HttpService,
private fieldsService: FieldsService
) {}
) { }

//fields
public async createFields(request: any, fieldsDto: FieldsDto) {
const response = await this.fieldsService.createFields(request,fieldsDto);
const response = await this.fieldsService.createFields(request, fieldsDto);
if (response?.data?.errors) {
return new ErrorResponse({
errorCode: response?.data?.errors[0]?.extensions?.code,
Expand Down Expand Up @@ -107,7 +106,7 @@ export class HasuraFieldsService implements IServicelocatorfields {

//field values
public async createFieldValues(request: any, fieldValuesDto: FieldValuesDto) {
const response = await this.fieldsService.createFieldValues(request,fieldValuesDto);
const response = await this.fieldsService.createFieldValues(request, fieldValuesDto);
if (response?.data?.errors) {
return new ErrorResponse({
errorCode: response?.data?.errors[0]?.extensions?.code,
Expand Down Expand Up @@ -240,4 +239,6 @@ export class HasuraFieldsService implements IServicelocatorfields {

return fieldValuesResponse;
}

public async getFieldOptions() { }
}
1 change: 0 additions & 1 deletion src/adapters/hasura/user.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class HasuraUserService implements IServicelocator {

}
public async getUsersDetailsById(userData: UserData, response:any) {}
public async getUsersDetailsByCohortId(userData: Record<string, string>, response:any) {}

public async checkAndAddUser(request: any, userDto: UserCreateDto) {
// try {
Expand Down
105 changes: 31 additions & 74 deletions src/adapters/postgres/cohort-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class PostgresCohortService {
@InjectRepository(UserTenantMapping)
private UserTenantMappingRepository: Repository<UserTenantMapping>,
private fieldsService: PostgresFieldsService,

) { }

public async getCohortList(
Expand Down Expand Up @@ -79,6 +80,9 @@ export class PostgresCohortService {

try {

const result = {
cohortData: {}
};
if (!isUUID(cohortId)) {
return APIResponse.error(
res,
Expand All @@ -88,68 +92,26 @@ export class PostgresCohortService {
(HttpStatus.BAD_REQUEST)
)
}
const checkData = await this.checkAuthAndValidData(cohortId);
const checkData = await this.checkIfCohortExist(cohortId);
const cohortDetails = await this.findCohortDetails(cohortId);
const contextType = cohortDetails.type;
result.cohortData = cohortDetails;

if (checkData === true) {
const result = await this.getCohortDataWithCustomfield(cohortId);
return APIResponse.success(res, apiId, result, (HttpStatus.OK), "Cohort details fetched succcessfully.");

} else {
return APIResponse.error(
res,
apiId,
`Cohort not found`,
'Invalid cohortId',
(HttpStatus.NOT_FOUND)
)
if (!checkData) {
return APIResponse.error(res, apiId, `Cohort either does not exist or is not active`, 'Invalid cohortId', (HttpStatus.NOT_FOUND))
}

let customFieldsArray = await this.getCohortDataWithCustomfield(cohortId, contextType);
result.cohortData['customFields'] = customFieldsArray;

return APIResponse.success(res, apiId, result, (HttpStatus.OK), "Cohort details fetched successfully.");

} catch (error) {
const errorMessage = error.message || 'Internal server error';
return APIResponse.error(res, apiId, "Internal Server Error", errorMessage, (HttpStatus.INTERNAL_SERVER_ERROR));
}
}

public async getCohortDataWithCustomfield(cohortId: string) {
const result = {
cohortData: {}
};

let customFieldsArray = [];

const [filledValues, cohortDetails, customFields] = await Promise.all([
this.findFilledValues(cohortId),
this.findCohortDetails(cohortId),
this.findCustomFields()
]);


result.cohortData = cohortDetails;
const filledValuesMap = new Map(filledValues.map(item => [item.fieldId, item.value]));
for (let data of customFields) {
const fieldValue = filledValuesMap.get(data.fieldId);
const customField = {
fieldId: data.fieldId,
label: data.label,
value: fieldValue || '',
options: data?.fieldParams?.['options'] || {},
type: data.type || ''
};
customFieldsArray.push(customField);
}
result.cohortData['customFields'] = customFieldsArray;
return result
}


async findFilledValues(cohortId: string) {
let query = `SELECT C."cohortId",F."fieldId",F."value" FROM public."Cohort" C
LEFT JOIN public."FieldValues" F
ON C."cohortId" = F."itemId" where C."cohortId" =$1`;
let result = await this.cohortRepository.query(query, [cohortId]);
return result;
}

async findCohortDetails(cohortId: string) {
let whereClause: any = { cohortId: cohortId };
let cohortDetails = await this.cohortRepository.findOne({
Expand All @@ -158,16 +120,13 @@ export class PostgresCohortService {
return new ReturnResponseBody(cohortDetails);
}

async findCustomFields() {
let customFields = await this.fieldsRepository.find({
where: {
context: 'COHORT',
contextType: 'COHORT'
}
})
return customFields;
public async getCohortDataWithCustomfield(cohortId: string, contextType?: string) {
let context = 'COHORT';
let fieldValue = await this.fieldsService.getFieldValuesData(cohortId, context, contextType);
return fieldValue
}


public async findCohortName(userId: any) {
let query = `SELECT c."name",c."cohortId",c."parentId"
FROM public."CohortMembers" AS cm
Expand Down Expand Up @@ -203,8 +162,6 @@ export class PostgresCohortService {
return { valid: true, fieldId: "true" };
};



public async createCohort(request: any, cohortCreateDto: CohortCreateDto, res) {
const apiId = APIID.COHORT_CREATE;

Expand Down Expand Up @@ -344,7 +301,7 @@ export class PostgresCohortService {
)
}

const checkData = await this.checkAuthAndValidData(cohortId);
const checkData = await this.checkIfCohortExist(cohortId);

if (checkData === true) {
let updateData = {};
Expand Down Expand Up @@ -554,7 +511,7 @@ export class PostgresCohortService {
cohortDetails: [],
};

let count=0
let count = 0

if (whereClause['userId']) {
const additionalFields = Object.keys(whereClause).filter(key => key !== 'userId');
Expand Down Expand Up @@ -584,33 +541,33 @@ export class PostgresCohortService {
(HttpStatus.BAD_REQUEST)
)
}
const [data,totalCount] = await this.cohortMembersRepository.findAndCount({
const [data, totalCount] = await this.cohortMembersRepository.findAndCount({
where: whereClause,
skip: offset,
});
const cohortData = data.slice(offset, offset + (limit));
count=totalCount
count = totalCount
for (let data of cohortData) {
let cohortDetails = await this.getCohortDataWithCustomfield(data.cohortId);
results.cohortDetails.push(cohortDetails);
}
} else {
const [data,totalcount] = await this.cohortRepository.findAndCount({
const [data, totalcount] = await this.cohortRepository.findAndCount({
where: whereClause,
skip: offset
});
const cohortData = data.slice(offset, offset + (limit));
count=totalcount
count = totalcount

for (let data of cohortData) {
let cohortDetails = await this.getCohortDataWithCustomfield(data.cohortId);
let cohortDetails = await this.getCohortDataWithCustomfield(data.cohortId, data.type);
results.cohortDetails.push(cohortDetails);
}
}

if (results.cohortDetails.length > 0) {
const totalCount = results.cohortDetails.length
return APIResponse.success(response, apiId, {count,results}, (HttpStatus.OK), "Cohort details fetched successfully");
return APIResponse.success(response, apiId, { count, results }, (HttpStatus.OK), "Cohort details fetched successfully");

} else {
return APIResponse.error(
Expand Down Expand Up @@ -650,7 +607,7 @@ export class PostgresCohortService {
(HttpStatus.BAD_REQUEST)
)
}
const checkData = await this.checkAuthAndValidData(cohortId);
const checkData = await this.checkIfCohortExist(cohortId);

if (checkData === true) {
let query = `UPDATE public."Cohort"
Expand Down Expand Up @@ -683,7 +640,7 @@ export class PostgresCohortService {
}
}

public async checkAuthAndValidData(id: any) {
public async checkIfCohortExist(id: any) {

const existData = await this.cohortRepository.find({
where: {
Expand Down
32 changes: 10 additions & 22 deletions src/adapters/postgres/cohortMembers-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,12 @@ export class PostgresCohortMembersService {
let getUserDetails = await this.getUsers(where, options);

for (let data of getUserDetails) {
let userDetails = {
userId: data?.userId,
userName: data?.userName,
name: data?.name,
role: data?.role,
district: data?.district,
state: data?.state,
mobile: data?.mobile
};

if (fieldShowHide === "false") {
results.userDetails.push(userDetails);
results.userDetails.push(data);
} else {
const fieldValues = await this.getFieldandFieldValues(data.userId);
userDetails['customField'] = fieldValues;
results.userDetails.push(userDetails);
data['customField'] = fieldValues;
results.userDetails.push(data);
}
}

Expand Down Expand Up @@ -363,7 +353,7 @@ export class PostgresCohortMembersService {
}

if (isRoleCondition == 0) {
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile FROM public."CohortMembers" CM
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile, CM."memberStatus", CM."statusReason" FROM public."CohortMembers" CM
INNER JOIN public."Users" U
ON CM."userId" = U."userId"
INNER JOIN public."UserRolesMapping" UR
Expand All @@ -372,7 +362,7 @@ export class PostgresCohortMembersService {
ON R."roleId" = UR."roleId" ${whereCase} ${optionsCase}`;
}
else {
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile FROM public."CohortMembers" CM
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile, CM."memberStatus", CM."statusReason" FROM public."CohortMembers" CM
INNER JOIN public."Users" U
ON CM."userId" = U."userId"
INNER JOIN public."UserRolesMapping" UR
Expand All @@ -385,7 +375,6 @@ export class PostgresCohortMembersService {

}


public async updateCohortMembers(
cohortMembershipId: string,
loginUser: any,
Expand All @@ -397,21 +386,21 @@ export class PostgresCohortMembersService {
try {
cohortMembersUpdateDto.updatedBy = loginUser;
if (!isUUID(cohortMembershipId)) {
return APIResponse.error(res, apiId, "Bad Request", "Invalid input: Please Enter a valid UUID for cohortMemberId.", HttpStatus.BAD_REQUEST);
return APIResponse.error(res, apiId, "Bad Request", "Invalid input: Please Enter a valid UUID for cohortMembershipId.", HttpStatus.BAD_REQUEST);
}

const cohortMemberToUpdate = await this.cohortMembersRepository.findOne({
const cohortMembershipToUpdate = await this.cohortMembersRepository.findOne({
where: { cohortMembershipId: cohortMembershipId },
});

if (!cohortMemberToUpdate) {
if (!cohortMembershipToUpdate) {
return APIResponse.error(res, apiId, "Not Found", "Invalid input: Cohort member not found.", HttpStatus.NOT_FOUND);
}

Object.assign(cohortMemberToUpdate, cohortMembersUpdateDto);
Object.assign(cohortMembershipToUpdate, cohortMembersUpdateDto);

const updatedCohortMember = await this.cohortMembersRepository.save(
cohortMemberToUpdate
cohortMembershipToUpdate
);

return APIResponse.success(res, apiId, updatedCohortMember, HttpStatus.OK, "Cohort Member Updated successfully.");
Expand All @@ -422,7 +411,6 @@ export class PostgresCohortMembersService {
}
}


public async deleteCohortMemberById(
tenantid: any,
cohortMembershipId: any,
Expand Down
Loading

0 comments on commit a71771d

Please sign in to comment.