From e0f9c28a7d056c4b5127270314d787a84a7dc974 Mon Sep 17 00:00:00 2001 From: "Barros, Antonio" Date: Sat, 11 May 2024 11:06:53 -0300 Subject: [PATCH] Change create_at and updated_at to TIMESTAMP WITH TIME ZONE. --- package-lock.json | 10 ++++++ .../migrations/20240511135945_/migration.sql | 28 ++++++++++++++++ prisma/schema.prisma | 32 +++++++++---------- src/sessions/sessions.service.ts | 6 ++-- .../shelter-managers.service.ts | 2 +- src/shelter-managers/types.ts | 4 +-- src/shelter-supply/shelter-supply.service.ts | 8 ++--- src/shelter-supply/types.ts | 4 +-- src/shelter/shelter.service.ts | 6 ++-- src/shelter/types/types.ts | 4 +-- .../supply-categories.service.ts | 4 +-- src/supply-categories/types.ts | 4 +-- src/supply/supply.service.ts | 4 +-- src/supply/types.ts | 4 +-- src/users/types.ts | 4 +-- src/users/users.service.ts | 4 +-- 16 files changed, 83 insertions(+), 45 deletions(-) create mode 100644 prisma/migrations/20240511135945_/migration.sql diff --git a/package-lock.json b/package-lock.json index 6c3ae692..44e87aad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", + "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", @@ -2405,6 +2406,15 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/bcrypt": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-5.0.2.tgz", + "integrity": "sha512-6atioO8Y75fNcbmj0G7UjI9lXN2pQ/IGJ2FWT4a/btd0Lk9lQalHLKhkgKVZ3r+spnmWUKfbMi1GEe9wyHQfNQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/body-parser": { "version": "1.19.5", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", diff --git a/prisma/migrations/20240511135945_/migration.sql b/prisma/migrations/20240511135945_/migration.sql new file mode 100644 index 00000000..a24e755b --- /dev/null +++ b/prisma/migrations/20240511135945_/migration.sql @@ -0,0 +1,28 @@ +-- ALTER TABLE +ALTER TABLE "users" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "users" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "category_supplies" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "category_supplies" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "sessions" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "sessions" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "shelter_managers" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "shelter_managers" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "shelter_supplies" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "shelter_supplies" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "shelters" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "shelters" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + +-- ALTER TABLE +ALTER TABLE "supplies" ALTER COLUMN "created_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; +ALTER TABLE "supplies" ALTER COLUMN "updated_at" TYPE TIMESTAMP(3) WITH TIME ZONE USING TO_TIMESTAMP("updated_at", 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')::TIMESTAMP; + diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 792b1f65..90e74ba7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,8 +22,8 @@ model User { password String phone String @unique accessLevel AccessLevel @default(value: User) - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() sessions Session[] shelterManagers ShelterManagers[] @@ -37,8 +37,8 @@ model Session { ip String? userAgent String? @map("user_agent") active Boolean @default(value: true) - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() user User @relation(fields: [userId], references: [id]) @@ -48,8 +48,8 @@ model Session { model SupplyCategory { id String @id @default(uuid()) name String @unique - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() supplies Supply[] @@ -61,8 +61,8 @@ model ShelterSupply { supplyId String @map("supply_id") priority Int @default(value: 0) quantity Int? - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() shelter Shelter @relation(fields: [shelterId], references: [id]) supply Supply @relation(fields: [supplyId], references: [id]) @@ -75,8 +75,8 @@ model Supply { id String @id @default(uuid()) supplyCategoryId String @map("supply_category_id") name String - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id]) shelterSupplies ShelterSupply[] @@ -97,8 +97,8 @@ model Shelter { latitude Float? longitude Float? verified Boolean @default(value: false) - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() shelterManagers ShelterManagers[] shelterSupplies ShelterSupply[] @@ -107,10 +107,10 @@ model Shelter { } model ShelterManagers { - shelterId String @map("shelter_id") - userId String @map("user_id") - createdAt String @map("created_at") @db.VarChar(32) - updatedAt String? @map("updated_at") @db.VarChar(32) + shelterId String @map("shelter_id") + userId String @map("user_id") + createdAt DateTime @default(value: now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime? @map("updated_at") @db.Timestamptz() user User @relation(fields: [userId], references: [id]) shelter Shelter @relation(fields: [shelterId], references: [id]) diff --git a/src/sessions/sessions.service.ts b/src/sessions/sessions.service.ts index 235d7f0d..68c4976b 100644 --- a/src/sessions/sessions.service.ts +++ b/src/sessions/sessions.service.ts @@ -27,7 +27,7 @@ export class SessionsService { }, data: { active: false, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); @@ -36,7 +36,7 @@ export class SessionsService { userId: user.id, ip, userAgent, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); @@ -73,7 +73,7 @@ export class SessionsService { userId, }, data: { - updatedAt: new Date().toISOString(), + updatedAt: new Date(), active: false, }, }); diff --git a/src/shelter-managers/shelter-managers.service.ts b/src/shelter-managers/shelter-managers.service.ts index 648e9719..68bac864 100644 --- a/src/shelter-managers/shelter-managers.service.ts +++ b/src/shelter-managers/shelter-managers.service.ts @@ -14,7 +14,7 @@ export class ShelterManagersService { data: { shelterId, userId, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } diff --git a/src/shelter-managers/types.ts b/src/shelter-managers/types.ts index 92a5eb85..ecb5029a 100644 --- a/src/shelter-managers/types.ts +++ b/src/shelter-managers/types.ts @@ -3,8 +3,8 @@ import z from 'zod'; const ShelterManagerSchema = z.object({ shelterId: z.string(), userId: z.string(), - createdAt: z.string(), - updatedAt: z.string().optional().nullable(), + createdAt: z.date(), + updatedAt: z.date().optional().nullable(), }); const CreateShelterManagerSchema = ShelterManagerSchema.pick({ diff --git a/src/shelter-supply/shelter-supply.service.ts b/src/shelter-supply/shelter-supply.service.ts index 73faddc4..bf0cab6b 100644 --- a/src/shelter-supply/shelter-supply.service.ts +++ b/src/shelter-supply/shelter-supply.service.ts @@ -26,7 +26,7 @@ export class ShelterSupplyService { prioritySum: { increment: newPriority - oldPriority, }, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } @@ -41,7 +41,7 @@ export class ShelterSupplyService { priority, supplyId, quantity: priority !== SupplyPriority.UnderControl ? quantity : null, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } @@ -74,7 +74,7 @@ export class ShelterSupplyService { data: { ...data, quantity: priority !== SupplyPriority.UnderControl ? quantity : null, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } @@ -91,7 +91,7 @@ export class ShelterSupplyService { }, data: { priority: SupplyPriority.UnderControl, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } diff --git a/src/shelter-supply/types.ts b/src/shelter-supply/types.ts index 1ff25f90..d1ea6c2c 100644 --- a/src/shelter-supply/types.ts +++ b/src/shelter-supply/types.ts @@ -13,8 +13,8 @@ const ShelterSupplySchema = z.object({ z.literal(SupplyPriority.Urgent), ]), quantity: z.number().gt(0).nullable().optional(), - createdAt: z.string(), - updatedAt: z.string().nullable().optional(), + createdAt: z.date(), + updatedAt: z.date().nullable().optional(), }); const CreateShelterSupplySchema = ShelterSupplySchema.pick({ diff --git a/src/shelter/shelter.service.ts b/src/shelter/shelter.service.ts index a24aa7ab..4661b31e 100644 --- a/src/shelter/shelter.service.ts +++ b/src/shelter/shelter.service.ts @@ -29,7 +29,7 @@ export class ShelterService { await this.prismaService.shelter.create({ data: { ...payload, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } @@ -42,7 +42,7 @@ export class ShelterService { }, data: { ...payload, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } @@ -55,7 +55,7 @@ export class ShelterService { }, data: { ...payload, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } diff --git a/src/shelter/types/types.ts b/src/shelter/types/types.ts index b3911c96..a052a9ed 100644 --- a/src/shelter/types/types.ts +++ b/src/shelter/types/types.ts @@ -19,8 +19,8 @@ const ShelterSchema = z.object({ capacity: z.number().min(0).nullable().optional(), contact: z.string().nullable().optional(), verified: z.boolean(), - createdAt: z.string(), - updatedAt: z.string().nullable().optional(), + createdAt: z.date(), + updatedAt: z.date().nullable().optional(), }); const CreateShelterSchema = ShelterSchema.omit({ diff --git a/src/supply-categories/supply-categories.service.ts b/src/supply-categories/supply-categories.service.ts index 4ebd796a..a1d6dece 100644 --- a/src/supply-categories/supply-categories.service.ts +++ b/src/supply-categories/supply-categories.service.ts @@ -16,7 +16,7 @@ export class SupplyCategoriesService { await this.prismaService.supplyCategory.create({ data: { ...payload, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } @@ -29,7 +29,7 @@ export class SupplyCategoriesService { }, data: { ...payload, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } diff --git a/src/supply-categories/types.ts b/src/supply-categories/types.ts index b096e2c0..05b5ff9a 100644 --- a/src/supply-categories/types.ts +++ b/src/supply-categories/types.ts @@ -11,8 +11,8 @@ export enum SupplyStatus { const SupplyCategorySchema = z.object({ id: z.string(), name: z.string().transform(capitalize), - createdAt: z.string(), - updatedAt: z.string().nullable().optional(), + createdAt: z.date(), + updatedAt: z.date().nullable().optional(), }); const CreateSupplyCategorySchema = SupplyCategorySchema.pick({ diff --git a/src/supply/supply.service.ts b/src/supply/supply.service.ts index a3caf57e..df835238 100644 --- a/src/supply/supply.service.ts +++ b/src/supply/supply.service.ts @@ -13,7 +13,7 @@ export class SupplyService { return await this.prismaService.supply.create({ data: { ...payload, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } @@ -26,7 +26,7 @@ export class SupplyService { }, data: { ...payload, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); } diff --git a/src/supply/types.ts b/src/supply/types.ts index faef2d11..e4ca6244 100644 --- a/src/supply/types.ts +++ b/src/supply/types.ts @@ -12,8 +12,8 @@ const SupplySchema = z.object({ id: z.string(), supplyCategoryId: z.string(), name: z.string().transform(capitalize), - createdAt: z.string(), - updatedAt: z.string().nullable().optional(), + createdAt: z.date(), + updatedAt: z.date().nullable().optional(), }); const CreateSupplySchema = SupplySchema.omit({ diff --git a/src/users/types.ts b/src/users/types.ts index 1721f4bf..04f89b23 100644 --- a/src/users/types.ts +++ b/src/users/types.ts @@ -11,8 +11,8 @@ const UserSchema = z.object({ password: z.string(), phone: z.string().transform(removeNotNumbers), accessLevel: z.nativeEnum(AccessLevel), - createdAt: z.string(), - updatedAt: z.string().nullable().optional(), + createdAt: z.date(), + updatedAt: z.date().nullable().optional(), }); const CreateUserSchema = UserSchema.pick({ diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 49e5df91..f2c5da1c 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -16,7 +16,7 @@ export class UsersService { phone, password: phone, login: phone, - createdAt: new Date().toISOString(), + createdAt: new Date(), }, }); } @@ -29,7 +29,7 @@ export class UsersService { }, data: { ...payload, - updatedAt: new Date().toISOString(), + updatedAt: new Date(), }, }); }