Skip to content

Commit

Permalink
Add createdAt date time to maps (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenjohanson authored Aug 23, 2024
1 parent 94020a0 commit 71ccc9c
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions teammapper-backend/src/jobs/seedMapData.job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const createMap = (nodes: IMmpClientNode[]): IMmpClientMap => {
return {
uuid: crypto.randomUUID(),
lastModified: new Date(),
createdAt: new Date(),
deleteAfterDays: 30,
data: nodes,
deletedAt: new Date(),
Expand Down
3 changes: 3 additions & 0 deletions teammapper-backend/src/map/entities/mmpMap.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export class MmpMap {
})
/* eslint-enable @typescript-eslint/no-unused-vars */
nodes: MmpNode[]

@Column({ type: 'timestamptz', default: () => 'now()' })
createdAt: Date
}
3 changes: 3 additions & 0 deletions teammapper-backend/src/map/entities/mmpNode.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ export class MmpNode {

@Column({ type: 'timestamptz', nullable: true, default: () => 'now()' })
lastModified: Date

@Column({ type: 'timestamptz', default: () => 'now()' })
createdAt: Date
}
1 change: 1 addition & 0 deletions teammapper-backend/src/map/services/maps.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('MapsController', () => {
coordinatesX: 3,
coordinatesY: 1,
lastModified: lastModified,
createdAt: new Date()
})
}

Expand Down
11 changes: 6 additions & 5 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class MapsService {
private nodesRepository: Repository<MmpNode>,
@InjectRepository(MmpMap)
private mapsRepository: Repository<MmpMap>
) {}
) { }

findMap(uuid: string): Promise<MmpMap | null> {
if(!uuidValidate(uuid)) return Promise.reject(new MalformedUUIDError('Invalid UUID'))
if (!uuidValidate(uuid)) return Promise.reject(new MalformedUUIDError('Invalid UUID'))

return this.mapsRepository.findOne({
where: { id: uuid },
Expand All @@ -46,7 +46,7 @@ export class MapsService {

const nodes: MmpNode[] = await this.findNodes(map?.id)
const days: number = configService.deleteAfterDays()

return mapMmpMapToClient(
map,
nodes,
Expand Down Expand Up @@ -136,6 +136,7 @@ export class MapsService {
...existingNode,
...mapClientNodeToMmpNode(clientNode, mapId),
lastModified: new Date(),
createdAt: new Date()
})
}

Expand Down Expand Up @@ -213,7 +214,7 @@ export class MapsService {
return copyDate
}

async deleteOutdatedMaps(afterDays: number = 30): Promise<number | null | undefined> {
async deleteOutdatedMaps(afterDays: number = 30): Promise<number | null | undefined> {
const today = new Date()

const deleteQuery = this.mapsRepository
Expand Down Expand Up @@ -271,7 +272,7 @@ export class MapsService {

async validatesNodeParentForNode(
mapId: string,
node: MmpNode
node: MmpNode
): Promise<boolean> {
return (
node.root ||
Expand Down
3 changes: 2 additions & 1 deletion teammapper-backend/src/map/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export interface IMmpClientMap {
deleteAfterDays: number
deletedAt: Date
data: IMmpClientNode[]
options: IMmpClientMapOptions
options: IMmpClientMapOptions,
createdAt: Date
}

export interface IMmpClientPrivateMap {
Expand Down
1 change: 1 addition & 0 deletions teammapper-backend/src/map/utils/clientServerMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const mapMmpMapToClient = (
deletedAt,
lastModified: serverMap.lastModified,
options: serverMap?.options,
createdAt: serverMap.createdAt
}
}

Expand Down
2 changes: 2 additions & 0 deletions teammapper-backend/src/map/utils/tests/mapFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const createMmpMap = (overrides = {}): MmpMap => ({
fontMinSize: 1,
fontIncrement: 1
},
createdAt: new Date('1970-01-01'),
nodes: Array<MmpNode>(),
...overrides
})
Expand All @@ -29,6 +30,7 @@ export const createMmpClientMap = (overrides = {}): IMmpClientMap => ({
fontMinSize: 1,
fontIncrement: 1
},
createdAt: new Date('1970-01-01'),
...overrides,
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class AddCreatedAtToMap1724314314717 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "mmp_map" ADD "createdAt" TIMESTAMP WITH TIME ZONE`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_map" DROP COLUMN "createdAt"`)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class AddCreatedAtToNode1724314435583 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "mmp_node" ADD "createdAt" TIMESTAMP WITH TIME ZONE`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_node" DROP COLUMN "createdAt"`)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddDefaultToCreatedAtMmpMap1724325535133 implements MigrationInterface {
name = 'AddDefaultToCreatedAtMmpMap1724325535133'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_map" ALTER COLUMN "createdAt" SET DEFAULT now()`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_map" ALTER COLUMN "createdAt" DROP DEFAULT`);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddDefaultToCreatedAtMmpNode1724325567562 implements MigrationInterface {
name = 'AddDefaultToCreatedAtMmpNode1724325567562'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_node" ALTER COLUMN "createdAt" SET DEFAULT now()`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mmp_node" ALTER COLUMN "createdAt" DROP DEFAULT`);
}

}
1 change: 1 addition & 0 deletions teammapper-backend/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('AppController (e2e)', () => {
deleteAfterDays: 30,
deletedAt: new Date(),
options: { fontMaxSize: 10, fontMinSize: 15, fontIncrement: 2 },
createdAt: new Date(),
data: [
{
name: 'test',
Expand Down
1 change: 1 addition & 0 deletions teammapper-frontend/mmp/src/map/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface MapCreateEvent {
interface MapProperties {
uuid: string,
lastModified: number,
createdAt: number,
data: MapSnapshot,
deletedAt: number,
deleteAfterDays: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class MapSyncService implements OnDestroy {
modificationSecret: privateServerMap.modificationSecret,
ttl: serverMap.deletedAt,
rootName: serverMap.data[0].name,
createdAt: serverMap.createdAt,
});

this.prepareMap(serverMap);
Expand Down Expand Up @@ -191,6 +192,7 @@ export class MapSyncService implements OnDestroy {
const cachedMap: CachedMap = {
data: this.mmpService.exportAsJSON(),
lastModified: Date.now(),
createdAt: cachedMapEntry.cachedMap.createdAt,
uuid: cachedMapEntry.cachedMap.uuid,
deletedAt: cachedMapEntry.cachedMap.deletedAt,
deleteAfterDays: cachedMapEntry.cachedMap.deleteAfterDays,
Expand Down Expand Up @@ -350,6 +352,7 @@ export class MapSyncService implements OnDestroy {
return Object.assign({}, serverMap, {
lastModified: Date.parse(serverMap.lastModified),
deletedAt: Date.parse(serverMap.deletedAt),
createdAt: Date.parse(serverMap.createdAt),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface ServerMap {
deleteAfterDays: number;
data: MapSnapshot;
options: CachedMapOptions;
createdAt: string;
}

interface PrivateServerMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface CachedMapEntry {

export interface CachedMap {
lastModified: number;
createdAt: number;
data: MapSnapshot;
uuid: string;
deleteAfterDays: number;
Expand Down

0 comments on commit 71ccc9c

Please sign in to comment.