Skip to content

Commit

Permalink
Logic fix
Browse files Browse the repository at this point in the history
  • Loading branch information
smile0711 authored and dburleson committed Jan 15, 2024
1 parent 45422a5 commit c3c2c92
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 56 deletions.
57 changes: 37 additions & 20 deletions api/src/services/block/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ export class BlockService {
private roleRepository: RoleRepository,
private memberRepository: MemberRepository,
) {}


private async updateDeputyId(deputy: any): Promise<number> {
let newDeputies = 0;
if (deputy.username !== null) {
const result = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies = result[0].id;
}
return newDeputies;
}

public async find(blockId: number): Promise<Place> {
return await this.blockRepository.find(blockId);
}
Expand Down Expand Up @@ -65,24 +74,23 @@ export class BlockService {
newOwner = 0;
}
if (newOwner !== 0) {
if (oldOwner !== 0){
if (oldOwner !== 0) {
await this.blockRepository.removeIdFromAssignment(blockId, oldOwner, ownerCode);
const response: any = await this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
await this.memberRepository.update(oldOwner, {primary_role_id: null});
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
await this.memberRepository.update(oldOwner, {primary_role_id: null});
}
}
}
await this.blockRepository.addIdToAssignment(blockId, newOwner, ownerCode);
}
data.deputies.forEach((deputies, index) => {
oldDeputies[index] = deputies.member_id;
});
for (const [index, deputy] of givenDeputies.entries()) try {
const result: any = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies[index] = result[0].id;
} catch (error) {
break;
for (let i = 0; i < givenDeputies.length; i++) {
newDeputies[i] = await this.updateDeputyId(givenDeputies[i]);
}
oldDeputies.forEach((oldDeputies, index) => {
if (oldDeputies !== newDeputies[index]) {
Expand All @@ -92,27 +100,36 @@ export class BlockService {
} catch (e) {
console.log(e);
}
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
if (oldDeputies !== 0) {
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (primaryRoleId && deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
}
} else {
try {
this.blockRepository.removeIdFromAssignment(blockId, oldDeputies, deputyCode);
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
}
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
this.blockRepository.addIdToAssignment(blockId, newDeputies[index], deputyCode);
} catch (e) {
console.log(e);
}
}
}
});
return;
}

public async getMapLocationAndPlaces(blockId: number): Promise<any> {
Expand Down
53 changes: 36 additions & 17 deletions api/src/services/colony/colony.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Service } from 'typedi';

import { ColonyRepository, RoleAssignmentRepository, RoleRepository, MemberRepository } from '../../repositories';
import { Place } from '../../types/models';
import * as console from 'console';

/** Service for dealing with colony */
@Service()
Expand All @@ -12,7 +13,19 @@ export class ColonyService {
private roleRepository: RoleRepository,
private memberRepository: MemberRepository,
) {}


private async updateDeputyId(deputy: any): Promise<number> {
let newDeputies = 0;
console.log(deputy.username);
if (deputy.username !== null) {
if (deputy.username.length > 0) {
const result = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies = result[0].id;
}
}
return newDeputies;
}

public async find(colonyId: number): Promise<Place> {
return await this.colonyRepository.find(colonyId);
}
Expand Down Expand Up @@ -57,7 +70,7 @@ export class ColonyService {
if (oldOwner !== 0) {
await this.colonyRepository.removeIdFromAssignment(colonyId, oldOwner, ownerCode);
const response: any = await this.memberRepository.getPrimaryRoleName(oldOwner);
if (response) {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
await this.memberRepository.update(oldOwner, {primary_role_id: null});
Expand All @@ -69,11 +82,8 @@ export class ColonyService {
data.deputies.forEach((deputies, index) => {
oldDeputies[index] = deputies.member_id;
});
for (const [index, deputy] of givenDeputies.entries()) try {
const result: any = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies[index] = result[0].id;
} catch (error) {
break;
for (let i = 0; i < givenDeputies.length; i++) {
newDeputies[i] = await this.updateDeputyId(givenDeputies[i]);
}
oldDeputies.forEach((oldDeputies, index) => {
if (oldDeputies !== newDeputies[index]) {
Expand All @@ -83,27 +93,36 @@ export class ColonyService {
} catch (e) {
console.log(e);
}
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (primaryRoleId && ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
if (oldDeputies !== 0) {
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (primaryRoleId && deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
}
} else {
try {
this.colonyRepository.removeIdFromAssignment(colonyId, oldDeputies, deputyCode);
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
}
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
this.colonyRepository.addIdToAssignment(colonyId, newDeputies[index], deputyCode);
} catch (e) {
console.log(e);
}
}
}
});
return;
}

public async canAdmin(colonyId: number, memberId: number): Promise<boolean> {
Expand Down
55 changes: 36 additions & 19 deletions api/src/services/hood/hood.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export class HoodService {
private memberRepository: MemberRepository,
) {}

private async updateDeputyId(deputy: any): Promise<number> {
let newDeputies = 0;
if (deputy.username !== null) {
const result = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies = result[0].id;
}
return newDeputies;
}

public async find(hoodId: number): Promise<Place> {
return await this.hoodRepository.find(hoodId);
}
Expand Down Expand Up @@ -62,51 +71,59 @@ export class HoodService {
if (oldOwner !== 0) {
await this.hoodRepository.removeIdFromAssignment(hoodId, oldOwner, ownerCode);
const response: any = await this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
await this.memberRepository.update(oldOwner, {primary_role_id: null});
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
await this.memberRepository.update(oldOwner, {primary_role_id: null});
}
}
}
await this.hoodRepository.addIdToAssignment(hoodId, newOwner, ownerCode);
}
data.deputies.forEach((deputies, index) => {
oldDeputies[index] = deputies.member_id;
});
for (const [index, deputy] of givenDeputies.entries()) try {
const result: any = await this.memberRepository.findIdByUsername(deputy.username);
newDeputies[index] = result[0].id;
} catch (error) {
break;
for (let i = 0; i < givenDeputies.length; i++) {
newDeputies[i] = await this.updateDeputyId(givenDeputies[i]);
}
oldDeputies.forEach((oldDeputies, index) => {
if (oldDeputies !== newDeputies[index]) {
if (newDeputies[index] === 0) {
try {
this.hoodRepository.removeIdFromAssignment(hoodId, oldDeputies, deputyCode);
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
}
} catch (e) {
console.log(e);
}
if (oldDeputies !== 0) {
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (primaryRoleId && deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
}
} else {
try {
this.hoodRepository.removeIdFromAssignment(hoodId, oldDeputies, deputyCode);
const response: any = this.memberRepository.getPrimaryRoleName(oldOwner);
const primaryRoleId = response[0].primary_role_id;
if (ownerCode === primaryRoleId){
this.memberRepository.update(oldOwner, {primary_role_id: null});
}
this.memberRepository.getPrimaryRoleName(oldDeputies)
.then((response: any) => {
if (response.length !== 0) {
const primaryRoleId = response[0].primary_role_id;
if (deputyCode === primaryRoleId) {
this.memberRepository.update(oldDeputies, {primary_role_id: null});
}
}
});
this.hoodRepository.addIdToAssignment(hoodId, newDeputies[index], deputyCode);
} catch (e) {
console.log(e);
}
}
}
});
return;
}

public async getColony(hoodId: number): Promise<Place> {
Expand Down

0 comments on commit c3c2c92

Please sign in to comment.