Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

name in reference block #500

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 137 additions & 93 deletions frontend/src/app/Services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Observable } from 'rxjs';
import { ApiService } from '../Services/api.service';
import { HttpClient } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import {catchError, tap} from 'rxjs/operators';
import { catchError, tap } from 'rxjs/operators';
import { Story } from '../model/Story';
import { StepType } from '../model/StepType';
import { StoryService } from './story.service';
Expand All @@ -17,7 +17,7 @@ import { Scenario } from '../model/Scenario';
providedIn: 'root'
})
export class BlockService {

/**
* @ignore
*/
Expand Down Expand Up @@ -47,12 +47,19 @@ export class BlockService {
* Event emitter to unpack Block
*/
public unpackBlockEvent = new EventEmitter();

/**
* Stores an array of references blocks, before the reference is deleted
/**
* Event emitter to update reference Block name
*/
referencesBlocks: Block[] = [];
public updateNameRefEvent = new EventEmitter();

/**
* Stores an array of references blocks, before the reference is deleted
*/
referencesBlocks: Block[] = [];
/**
* Scenarios to update when reference block is completely deleted
*/
scenariosToUpdate: Scenario[];
referenceStories: Story[];
referenceScenarios: Scenario[];
block: Block;
Expand All @@ -71,13 +78,13 @@ export class BlockService {
updateBlocksEmitter() {
this.updateBlocksEvent.emit();
}

/**
* Emits the delete block in blocks
*/
public deleteBlockEmitter() {
this.deleteBlockEvent.emit();
}
}
/**
* Emits the unpack block event
* @param block
Expand All @@ -86,6 +93,13 @@ export class BlockService {
this.unpackBlockEvent.emit(block);
}
/**
* Emits the update a reference block name event
* @param block
*/
public updateNameRefEmitter(block) {
this.updateNameRefEvent.emit(block);
}
/**
* Emits the checking stories for references event
* @param blockReferenceId
*/
Expand All @@ -107,25 +121,25 @@ export class BlockService {
*/
getBlocks(repoId: string): Observable<Block[]> {
const str = this.apiService.apiServer + '/block/getBlocks/' + repoId;
return this.http.get<Block[]>(str, ApiService.getOptions())
.pipe(tap(_ => {
//
}),
catchError(this.apiService.handleError));
return this.http.get<Block[]>(str, ApiService.getOptions())
.pipe(tap(_ => {
//
}),
catchError(this.apiService.handleError));
}
/**
* Updates a block
* @param blockTitle
* @param block
* @returns
*/
updateBlock(block: Block):Observable<Block>{
updateBlock(block: Block): Observable<Block> {
return this.http
.put<Block>(this.apiService.apiServer + '/block/' + block._id, block, ApiService.getOptions())
.pipe(tap(_ => {
.put<Block>(this.apiService.apiServer + '/block/' + block._id, block, ApiService.getOptions())
.pipe(tap(_ => {
//
}),
catchError(this.apiService.handleError));
}),
catchError(this.apiService.handleError));
}
/**
* Deletes a block
Expand All @@ -135,10 +149,10 @@ export class BlockService {
deleteBlock(blockId: string) {
const str = this.apiService.apiServer + '/block/' + blockId;
return this.http.delete<any>(str, ApiService.getOptions())
.pipe(tap(_ => {
//
}),
catchError(this.apiService.handleError));
.pipe(tap(_ => {
//
}),
catchError(this.apiService.handleError));
}
/**
* Saves a new block
Expand All @@ -147,20 +161,20 @@ export class BlockService {
*/
saveBlock(block: Block) {
return this.http
.post<any>(this.apiService.apiServer + '/block', block, ApiService.getOptions())
.pipe(tap(_ => {
//
}));
.post<any>(this.apiService.apiServer + '/block', block, ApiService.getOptions())
.pipe(tap(_ => {
//
}));
}
/**
* Delete central background-block
* @param block
* @param stories
*/
checkBackgroundsOnDelete(block, stories){
/**
* Delete central background-block
* @param block
* @param stories
*/
checkBackgroundsOnDelete(block, stories) {
let matchingStories = stories.filter((s) => s !== null && s.background.name === block.name);
if(matchingStories.length == 1){
this.deleteBlock(block._id).subscribe(_=>
if (matchingStories.length == 1) {
this.deleteBlock(block._id).subscribe(_ =>
this.updateBlocksEvent.emit()
)
}
Expand All @@ -178,31 +192,33 @@ export class BlockService {
catchError(this.apiService.handleError)
);
}
/**
* Search for a references in all stories
* @param stories
*/
searchReferences(stories: Story[]){
/**
* Search for a references in all stories
* @param stories
*/
searchReferences(stories: Story[]) {
this.referenceScenarios = [];
stories.filter((s) => s !== null).flatMap((story) => story.scenarios
stories = stories.filter((s) => s !== null);
stories.flatMap((story) => story.scenarios
.filter((scenario) => scenario.hasRefBlock))
.forEach((scenario) => this.referenceScenarios.push(scenario));

this.referenceStories = this.referenceScenarios
.map((scenario) => stories.find((story) => story.scenarios.includes(scenario)))
.filter((story, index, arr) => story && arr.indexOf(story) === index);
.map((scenario) => stories.find((story) => story.scenarios.includes(scenario)))
.filter((story, index, arr) => story && arr.indexOf(story) === index);
}
/**
* delete a reference and update Block
*/
deleteUpdateReferenceForBlock() {
if(this.referencesBlocks.length !== 0){
/**
* delete a reference and update Block
*/
deleteUpdateReferenceForBlock() {
if (this.referencesBlocks.length !== 0) {
for (const block of this.referencesBlocks) {
if (block.usedAsReference === false) {
delete block.usedAsReference;
this.updateBlock(block)
.subscribe(_ => {
this.updateBlocksEvent.emit();
});
.subscribe(_ => {
this.updateBlocksEvent.emit();
});
}
}
this.referencesBlocks = [];
Expand All @@ -216,29 +232,10 @@ export class BlockService {
* @param blocks
* @param stories
*/
checkBlockOnReference(blocks:Block[], stories: Story[], blockReferenceId){
if (!this.isBlockReferencedInScenarios(blockReferenceId,stories)) {
this.updateBlockReferenceStatus(blockReferenceId, blocks);
}
}

/**
* check if a block is referenced in any scenario step definitions
* @param blockReferenceId
* @param stories
*/
isBlockReferencedInScenarios(blockReferenceId: string, stories): boolean {
this.searchReferences(stories);
for (const scen of this.referenceScenarios) {
for (const prop in scen.stepDefinitions) {
for (let i = scen.stepDefinitions[prop].length - 1; i >= 0; i--) {
if (scen.stepDefinitions[prop][i]._blockReferenceId == blockReferenceId) {
return true;
}
}
}
checkBlockOnReference(blocks: Block[], stories: Story[], block: Block) {
if (!this.findRefBlockInScenarios(block, stories, 'checkOnReference')) {
this.updateBlockReferenceStatus(block._id, blocks);
}
return false;
}
/**
* update block reference status
Expand All @@ -253,43 +250,40 @@ export class BlockService {
}
}
}
/**
* Check reference and unpack block in all relevant stories
/**
* Check reference and unpack block in all relevant stories
* @param block
* @param stories
*/
deleteBlockReference(block, stories: Story[]) {
this.findRefBlockInScenarios(block, stories, "deleteBlockReference")
//update relevant stories after unpacking
this.updateReferenceStories();

}
/**
* Update scenarios after changes in reference blocks
* @param block
* @param stories
*/
deleteBlockReference(block, stories: Story[]) {
this.searchReferences(stories);
let scenariosToUpdate = [];
this.referenceScenarios.forEach((scenario) => {
for(const s in scenario.stepDefinitions){
scenario.stepDefinitions[s].forEach((element) => {
if(element._blockReferenceId == block._id){
this.unpackScenarioWithBlock(block, scenario);
scenariosToUpdate.push(scenario);
}
})
}
});
//update relevant stories after unpacking
if(scenariosToUpdate.length > 0){
updateReferenceStories() {
if (this.scenariosToUpdate.length > 0) {
this.referenceStories.forEach((story) => {
scenariosToUpdate.forEach((scenario) => {
this.scenariosToUpdate.forEach((scenario) => {
if (story.scenarios.includes(scenario)) {
this.storyService.updateStory(story).subscribe(_resp => {});
this.storyService.updateStory(story).subscribe(_resp => { });
}
});
});
}
}

/**
* Unpack steps from block. Wenn delete block unpack all reference in repository
* @param block
* @param scenario
*/
unpackScenarioWithBlock(block, scenario) {
delete block.usedAsReference;
delete block.usedAsReference;
if (block && block.stepDefinitions) {
for (const s in block.stepDefinitions) {
block.stepDefinitions[s].forEach((step: StepType, j) => {
Expand All @@ -304,4 +298,54 @@ export class BlockService {
}
}
}
/**
* Update the reference name in scenarios after changing the block name
* @param block
* @param stories
*/
updateNameReference(block: Block, stories: Story[]) {
if (this.findRefBlockInScenarios(block, stories, 'updateRefName')) {
this.updateReferenceStories();
} else {
console.error('Reference block not found');
}
}
/**
* Searching for a reference block in scenarios
* @param block
* @param stories
* @param event
*/
findRefBlockInScenarios(block: Block, stories: Story[], event: string) {
this.searchReferences(stories);
this.scenariosToUpdate = [];
let blockFound = false;

this.referenceScenarios.forEach((scenario) => {
for (const s in scenario.stepDefinitions) {
scenario.stepDefinitions[s].forEach((refStep) => {
if (refStep._blockReferenceId == block._id) {
switch (event) {
case 'updateRefName':
refStep.type = block.name;
this.scenariosToUpdate.push(scenario);
blockFound = true;
break;
case 'checkOnReference':
blockFound = true;
break;
case 'deleteBlockReference':
this.unpackScenarioWithBlock(block, scenario);
this.scenariosToUpdate.push(scenario);
blockFound = true;
break;
default:
break;
}
}
});
}
});
return blockFound;
}
}
Loading
Loading