Skip to content

Commit

Permalink
Merge pull request #447 from code4romania/feat/update-actions-archive
Browse files Browse the repository at this point in the history
feat: Actions archive mappings update + update trackings
  • Loading branch information
birloiflorian authored Oct 4, 2024
2 parents d5edac6 + 2ec832d commit 8d70102
Show file tree
Hide file tree
Showing 50 changed files with 198 additions and 25 deletions.
6 changes: 3 additions & 3 deletions backend/src/api/_mobile/news/presenters/news.presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { ActivityLogStatus } from 'src/modules/activity-log/enums/activity-log-s
export class NewsPresenter {
constructor(item: IActionArchiveModel) {
this.id = item.id;
this.organizationLogo = item.author.organization.logo;
this.organizationName = item.author.organization.name;
this.organizationLogo = item.organization.logo;
this.organizationName = item.organization.name;
this.activityLogId = (item.eventData as never)['activityLogId'];
this.contractId = (item.eventData as never)['documentContractId'];
this.accessRequestId = (item.eventData as never)['accessRequestId'];
this.organizationId = item.author.organization.id;
this.organizationId = item.organization.id;
this.eventName = item.eventName;
this.newStatus = (item.eventData as never)['newStatus'];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddOrganizationIdToActionsArchive1727938379738
implements MigrationInterface
{
name = 'AddOrganizationIdToActionsArchive1727938379738';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "actions_archive" ADD "organization_id" uuid`,
);
await queryRunner.query(
`ALTER TABLE "actions_archive" ADD CONSTRAINT "FK_fa13764b4b3b3f7064046a76d49" FOREIGN KEY ("organization_id") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);

const actionsArchive = await queryRunner.query(`
SELECT
actions_archive.*,
COALESCE(
"user".organization_id,
(actions_archive.event_data->>'organizationId')::uuid,
volunteer.organization_id
) AS organization_id
FROM actions_archive
LEFT JOIN "user" ON actions_archive.author_id = "user".id
LEFT JOIN volunteer ON (actions_archive.event_data->>'volunteerId')::uuid = volunteer.id
`);

for (const action of actionsArchive) {
if (action.organization_id) {
await queryRunner.query(
`UPDATE actions_archive
SET organization_id = $1
WHERE id = $2`,
[action.organization_id, action.id],
);
}
}

await queryRunner.query(
`ALTER TABLE "actions_archive" ALTER COLUMN "organization_id" SET NOT NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "actions_archive" DROP CONSTRAINT "FK_fa13764b4b3b3f7064046a76d49"`,
);
await queryRunner.query(
`ALTER TABLE "actions_archive" DROP COLUMN "organization_id"`,
);
}
}
2 changes: 2 additions & 0 deletions backend/src/modules/actions-archive/actions-archive.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export class ActionsArchiveFacade {
eventName: EventName,
eventData: TrackedEventData[EventName],
author: IAdminUserModel | IRegularUserModel,
organizationId: string,
changes?: unknown,
): void {
const event: CreateActionArchiveOptions = {
eventName,
eventData,
author,
changes,
organizationId,
};
this.eventEmitter.emit(TRACK_ACTION_EVENT, event);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseEntity } from 'src/infrastructure/base/base-entity';
import { AdminUserEntity } from 'src/modules/user/entities/user.entity';
import { UserEntity } from 'src/modules/user/entities/user.entity';
import {
Column,
Entity,
Expand All @@ -11,6 +11,7 @@ import {
TrackedEventData,
TrackedEventName,
} from '../enums/action-resource-types.enum';
import { OrganizationEntity } from 'src/modules/organization/entities/organization.entity';

@Entity({ name: 'actions_archive' })
export class ActionsArchiveEntity extends BaseEntity {
Expand All @@ -29,7 +30,14 @@ export class ActionsArchiveEntity extends BaseEntity {
@Column({ type: 'text', name: 'author_id' })
authorId: string;

@ManyToOne(() => AdminUserEntity)
@ManyToOne(() => UserEntity)
@JoinColumn({ name: 'author_id' })
author: AdminUserEntity;
author: UserEntity;

@Column({ type: 'text', name: 'organization_id' })
organizationId: string;

@ManyToOne(() => OrganizationEntity)
@JoinColumn({ name: 'organization_id' })
organization: OrganizationEntity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export enum TrackedEventName {
REJECT_DOCUMENT_CONTRACT_BY_NGO = 'REJECT_DOCUMENT_CONTRACT_BY_NGO',
REJECT_DOCUMENT_CONTRACT_BY_VOLUNTEER = 'REJECT_DOCUMENT_CONTRACT_BY_VOLUNTEER',
DELETE_DOCUMENT_CONTRACT = 'DELETE_DOCUMENT_CONTRACT',
EXPIRE_DOCUMENT_CONTRACT = 'EXPIRE_DOCUMENT_CONTRACT',

// New Templates
CREATE_DOCUMENT_TEMPLATE = 'CREATE_DOCUMENT_TEMPLATE',
Expand Down Expand Up @@ -238,4 +239,5 @@ export interface TrackedEventData {
[TrackedEventName.CREATE_DOCUMENT_TEMPLATE]: BaseDocumentTemplateActionsArchiveEvent;
[TrackedEventName.UPDATE_DOCUMENT_TEMPLATE]: BaseDocumentTemplateActionsArchiveEvent;
[TrackedEventName.DELETE_DOCUMENT_TEMPLATE]: BaseDocumentTemplateActionsArchiveEvent;
[TrackedEventName.EXPIRE_DOCUMENT_CONTRACT]: BaseDocumentContractActionsArchiveEvent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class ActionsArchiveEventListener {
eventData: event.eventData,
author: event.author,
changes: event.changes,
organizationId: event.organizationId,
}),
);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { IBaseModel } from 'src/common/interfaces/base.model';
import { IBasePaginationFilterModel } from 'src/infrastructure/base/base-pagination-filter.model';
import {
AdminUserTransformer,
IAdminUserModel,
} from 'src/modules/user/models/admin-user.model';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
import { ActionsArchiveEntity } from '../entities/actions-archive.entity';
import {
TrackedEventData,
TrackedEventName,
} from '../enums/action-resource-types.enum';
import { IRegularUserModel } from 'src/modules/user/models/regular-user.model';
import { NewsType } from '../enums/news-type.enum';
import {
BaseUserTransformer,
ICommonUserModel,
} from 'src/modules/user/models/base-user.model';
import {
IOrganizationModel,
OrganizationTransformer,
} from 'src/modules/organization/models/organization.model';

export const TRACK_ACTION_EVENT = 'track.action';

export interface IActionArchiveModel extends IBaseModel {
id: string;
author: IAdminUserModel;

author: ICommonUserModel;
organization: IOrganizationModel;
eventName: TrackedEventName;
eventData: TrackedEventData[TrackedEventName];

Expand All @@ -29,6 +34,7 @@ export type CreateActionArchiveOptions = Pick<
'eventName' | 'eventData' | 'changes'
> & {
author: IRegularUserModel | IAdminUserModel;
organizationId: string;
};

export type FindManyActionsArchiveOptions = {
Expand All @@ -51,7 +57,9 @@ export class ActionsArchiveTransformer {
return {
id: entity.id,

author: AdminUserTransformer.fromEntity(entity.author),
author: BaseUserTransformer.fromEntity(entity.author),

organization: OrganizationTransformer.fromEntity(entity.organization),

eventName: entity.eventName,
eventData: entity.eventData,
Expand All @@ -71,6 +79,8 @@ export class ActionsArchiveTransformer {

entity.authorId = action.author.id;

entity.organizationId = action.organizationId;

entity.changes = action.changes;

return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export class ActionsArchiveRepository
'author',
)
.select()
// TODO: Instead of filtering organization by author, add organizationId in the entity because otherwise we can't track volunteer actions
.where('author.organizationId = :organizationId', {
.where('actionsArchive.organizationId = :organizationId', {
organizationId: findOptions.organizationId,
})
.orderBy(
Expand Down Expand Up @@ -101,8 +100,8 @@ export class ActionsArchiveRepository
'author',
)
.leftJoinAndMapOne(
'author.organization',
'author.organization',
'actionsArchive.organization',
'actionsArchive.organization',
'organization',
)
.select()
Expand Down
23 changes: 23 additions & 0 deletions backend/src/modules/user/models/base-user.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { AdminUserEntity, UserEntity } from '../entities/user.entity';
import { UserType } from '../enums/user.enum';

export interface IUserModel {
id: string;
cognitoId: string;
Expand All @@ -6,3 +9,23 @@ export interface IUserModel {
phone: string;
// type: UserType;
}

export interface ICommonUserModel extends IUserModel {
type: UserType;
}

export class BaseUserTransformer {
static fromEntity(entity: UserEntity): ICommonUserModel {
if (!entity) return null;

return {
id: entity.id,
cognitoId: entity.cognitoId,
email: entity.email,
type:
entity instanceof AdminUserEntity ? UserType.ADMIN : UserType.REGULAR,
name: entity.name,
phone: entity.phone,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class ApproveAccessRequestUseCase
volunteerId: volunteer.id,
},
admin,
accessRequest.organizationId,
);

return updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class DeleteAccessRequestUseCase implements IUseCaseService<string> {
userId: accessRequest.requestedBy?.id,
},
admin,
admin.organizationId,
);

return deleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class RejectAccessRequestUseCase
userId: accessRequest.requestedBy?.id,
},
admin,
admin.organizationId,
);

return updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class ApproveActivityLogUsecase
newStatus: approved.status,
},
admin,
admin.organizationId,
);

return approved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class CreateActivityLogByAdmin
volunteerName: volunteer.user?.name,
},
admin,
admin.organizationId,
);

return created;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class CreateActivityLogByRegularUser
volunteerName: volunteer.user?.name,
},
user,
user.activeOrganization.id,
);

return created;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class RejectActivityLogUsecase
newStatus: rejected.status,
},
admin,
admin.organizationId,
);

return rejected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class ActivateActivityTypeUseCase
newStatus: updated.status,
},
admin,
admin.organizationId,
);

return updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class ArchiveActivityTypeUseCase
newStatus: updated.status,
},
admin,
admin.organizationId,
);

return updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class CreateActivityTypeUseCase
activityTypeName: created.name,
},
admin,
admin.organizationId,
);

return created;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class UpdateActivityTypeUseCase
activityTypeName: updated.name,
},
admin,
admin.organizationId,
ObjectDiff.diff(toUpdate, updated),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class CreateAnnouncementUseCase
status: newAnouncement.status,
},
admin,
admin.organizationId,
);

return newAnouncement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class DeleteAnnouncementUseCase implements IUseCaseService<string> {
announcementTitle: announcement.name,
},
admin,
admin.organizationId,
);

return deleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class UpdateAnnouncementUseCase
announcementTitle: updatedAnnouncement.name,
},
admin,
admin.organizationId,
);
}

Expand Down
1 change: 1 addition & 0 deletions backend/src/usecases/documents/create-contract.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class CreateContractUsecase implements IUseCaseService<IContractModel> {
contractNumber: contract.contractNumber,
},
admin,
organization.id,
);

return contract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class CreateDocumentContractUsecase implements IUseCaseService<string> {
documentTemplateName: template.name,
},
admin,
admin.organizationId,
);

return contract.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class CreateDocumentTemplateUsecase
documentTemplateName: newTemplate.name,
},
admin,
admin.organizationId,
);

return newTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class DeleteDocumentContractUsecase implements IUseCaseService<string> {
documentContractNumber: contract.documentNumber,
},
admin,
admin.organizationId,
);
} catch (error) {
if (error?.status === 400) {
Expand Down
Loading

0 comments on commit 8d70102

Please sign in to comment.