Skip to content

Commit

Permalink
Add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Jun 4, 2024
1 parent 0b1fc10 commit b6b8881
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('RecordPositionQueryFactory', () => {
it('should return query and params for FIND_BY_POSITION', async () => {
const positionValue = 1;
const queryType = RecordPositionQueryType.FIND_BY_POSITION;
const [query, params] = await factory.create(
const [query, params] = factory.create(
{ positionValue, recordPositionQueryType: queryType },
objectMetadataItem,
dataSourceSchema,
Expand All @@ -34,7 +34,7 @@ describe('RecordPositionQueryFactory', () => {

it('should return query and params for FIND_MIN_POSITION', async () => {
const queryType = RecordPositionQueryType.FIND_MIN_POSITION;
const [query, params] = await factory.create(
const [query, params] = factory.create(
{ recordPositionQueryType: queryType },
objectMetadataItem,
dataSourceSchema,
Expand All @@ -48,7 +48,7 @@ describe('RecordPositionQueryFactory', () => {

it('should return query and params for FIND_MAX_POSITION', async () => {
const queryType = RecordPositionQueryType.FIND_MAX_POSITION;
const [query, params] = await factory.create(
const [query, params] = factory.create(
{ recordPositionQueryType: queryType },
objectMetadataItem,
dataSourceSchema,
Expand All @@ -64,7 +64,7 @@ describe('RecordPositionQueryFactory', () => {
const positionValue = 1;
const recordId = '1';
const queryType = RecordPositionQueryType.UPDATE_POSITION;
const [query, params] = await factory.create(
const [query, params] = factory.create(
{ positionValue, recordId, recordPositionQueryType: queryType },
objectMetadataItem,
dataSourceSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('RecordPositionBackfillService', () => {

beforeEach(async () => {
recordPositionQueryFactory = {
create: jest.fn().mockResolvedValue(['query', []]),
create: jest.fn().mockReturnValue(['query', []]),
};

recordPositionFactory = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Injectable, Logger } from '@nestjs/common';

import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';

import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import {
RecordPositionQueryFactory,
RecordPositionQueryType,
} from 'src/engine/api/graphql/workspace-query-builder/factories/record-position-query.factory';
import { RecordPositionFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/record-position.factory';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { hasPositionField } from 'src/engine/metadata-modules/object-metadata/utils/has-position-field.util';

@Injectable()
export class RecordPositionBackfillService {
Expand All @@ -21,6 +20,10 @@ export class RecordPositionBackfillService {
) {}

async backfill(workspaceId: string, dryRun: boolean) {
this.logger.log(
`Starting backfilling record positions for workspace ${workspaceId}`,
);

const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);

Expand All @@ -34,7 +37,7 @@ export class RecordPositionBackfillService {

for (const objectMetadata of objectMetadataWithPosition) {
const [recordsWithoutPositionQuery, recordsWithoutPositionQueryParams] =
await this.recordPositionQueryFactory.create(
this.recordPositionQueryFactory.create(
{
recordPositionQueryType: RecordPositionQueryType.FIND_BY_POSITION,
positionValue: null,
Expand All @@ -48,12 +51,21 @@ export class RecordPositionBackfillService {
recordsWithoutPositionQuery,
recordsWithoutPositionQueryParams,
workspaceId,
undefined,
);

if (recordsWithoutPosition.length === 0) {
this.logger.log(
`No records without position for ${objectMetadata.nameSingular}`,
);
continue;
}

const position = await this.recordPositionFactory.create(
'last',
objectMetadata as ObjectMetadataInterface,
{
isCustom: objectMetadata.isCustom,
nameSingular: objectMetadata.nameSingular,
},
workspaceId,
);

Expand All @@ -63,6 +75,14 @@ export class RecordPositionBackfillService {
recordIndex++
) {
const recordId = recordsWithoutPosition[recordIndex].id;

if (!recordId) {
this.logger.log(
`Fetched record without id for ${objectMetadata.nameSingular}`,
);
continue;
}

const backfilledPosition = position + recordIndex;

this.logger.log(
Expand All @@ -73,7 +93,7 @@ export class RecordPositionBackfillService {
continue;
}

const [query, params] = await this.recordPositionQueryFactory.create(
const [query, params] = this.recordPositionQueryFactory.create(
{
recordPositionQueryType: RecordPositionQueryType.UPDATE_POSITION,
recordId: recordsWithoutPosition[recordIndex].id,
Expand All @@ -87,13 +107,8 @@ export class RecordPositionBackfillService {
query,
params,
workspaceId,
undefined,
);
}
}
}
}

const hasPositionField = (objectMetadata: ObjectMetadataInterface) =>
['company', 'opportunity', 'person'].includes(objectMetadata.nameSingular) ||
objectMetadata.isCustom;

0 comments on commit b6b8881

Please sign in to comment.