Skip to content

Commit

Permalink
Merge branch 'FE-Pagination' into testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Jun 26, 2024
2 parents baa5fed + 05f5186 commit d5eecf6
Show file tree
Hide file tree
Showing 34 changed files with 647 additions and 162 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ jobs:
with:
name: lib-schema
path: packages/lib/**/schema/
- uses: actions/upload-artifact@master
with:
name: backend-schema
path: packages/backend/src/schema/storage
backend-test:
runs-on: ubuntu-latest
needs: build
defaults:
run:
working-directory: 'packages/lib'
working-directory: 'packages/backend'
steps:
- uses: actions/checkout@v1
- id: workspace-test
Expand All @@ -61,7 +65,7 @@ jobs:
needs: build
defaults:
run:
working-directory: 'packages/lib'
working-directory: 'packages/delivery-service'
steps:
- uses: actions/checkout@v1
- id: workspace-test
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/migrations/20240624124318_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- The `createdAt` column on the `EncryptedMessage` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- AlterTable
ALTER TABLE "EncryptedMessage" DROP COLUMN "createdAt",
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
2 changes: 2 additions & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"start-inspect": "node --inspect=0.0.0.0:9229 ./dist/index.js",
"test": "yarn run before:tests && DATABASE_URL='postgresql://prisma:prisma@localhost:5433/tests?schema=public' yarn jest --coverage --runInBand --transformIgnorePatterns 'node_modules/(?!(dm3-lib-\\w*)/)'",
"build": "yarn tsc && cp ./config.yml ./dist/config.yml | true",
"build:schema": "sh ./schemas.sh",
"createDeliveryServiceProfile": "node --no-warnings ./cli.js",
"before:tests": "docker-compose -f docker-compose.test.yml up -d && DATABASE_URL='postgresql://prisma:prisma@localhost:5433/tests?schema=public' yarn prisma-init"
},
Expand All @@ -53,6 +54,7 @@
"prettier": "^2.6.2",
"superagent": "^8.0.3",
"supertest": "^6.3.1",
"ts-json-schema-generator": "^0.98.0",
"ts-node": "^10.9.1",
"typescript": "^4.4.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ generator client {

model EncryptedMessage {
id String @id
createdAt Int @default(0)
createdAt DateTime @default(now())
encryptedEnvelopContainer String
encryptedContactName String
conversationId String
Expand Down
7 changes: 7 additions & 0 deletions packages/backend/schemas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
yarn ts-json-schema-generator -f tsconfig.json --path schema/storage/EditMessageBatchRequest.ts --type _EditMessageBatchRequest -o ./src/schema/storage/EditMessageBatchRequest.schema.json --no-type-check \

yarn ts-json-schema-generator -f tsconfig.json --path schema/storage/AddMessageBatchRequest.ts --type _AddMessageBatchRequest -o ./src/schema/storage/AddMessageBatchRequest.schema.json --no-type-check \

yarn ts-json-schema-generator -f tsconfig.json --path schema/storage/AddMessageRequest.ts --type _AddMessageRequest -o ./src/schema/storage/AddMessageRequest.schema.json --no-type-check \

yarn ts-json-schema-generator -f tsconfig.json --path schema/storage/PaginatedRequest.ts --type _PaginatedRequest -o ./src/schema/storage/PaginatedRequest.schema.json --no-type-check \
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export const addMessageBatch =
//store each message in the db
const createMessagePromises = messageBatch.map(
({ messageId, createdAt, encryptedEnvelopContainer }) => {
//The database stores the date as an ISO 8601 string. Hence we need to convert it to a Date object
const createAtDate = new Date(createdAt);
return db.encryptedMessage.create({
data: {
ownerId: account.id,
id: messageId,
createdAt,
createdAt: createAtDate,
conversationId: conversation.id,
encryptedContactName,
encryptedEnvelopContainer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { PrismaClient } from '@prisma/client';
import { ConversationRecord } from './dto/ConversationRecord';
import { GetResult } from '@prisma/client/runtime';
export const getConversationList =
(db: PrismaClient) =>
async (
ensName: string,
size: number,
pagesize: number,
offset: number,
): Promise<ConversationRecord[]> => {
//Find the account first we want to get the conversations for
Expand All @@ -21,9 +20,9 @@ export const getConversationList =

const conversations = await db.conversation.findMany({
//The pages that have to be skipped
skip: offset * size,
skip: offset * pagesize,
//The requested page size
take: size,
take: pagesize,
where: {
accountId: account.id,
isHidden: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getMessages =
async (
ensName: string,
encryptedContactName: string,
size: number,
pageSize: number,
offset: number,
) => {
//Find the account first we want to get the messages for
Expand Down Expand Up @@ -33,8 +33,8 @@ export const getMessages =

try {
const messageRecord = await db.encryptedMessage.findMany({
skip: offset * size,
take: size,
skip: offset * pageSize,
take: pageSize,
where: {
ownerId: account.id,
encryptedContactName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$ref": "#/definitions/_AddMessageBatchRequest",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MessageRecord": {
"additionalProperties": false,
"properties": {
"createdAt": {
"type": "number"
},
"encryptedEnvelopContainer": {
"type": "string"
},
"messageId": {
"type": "string"
}
},
"required": [
"createdAt",
"messageId",
"encryptedEnvelopContainer"
],
"type": "object"
},
"_AddMessageBatchRequest": {
"additionalProperties": false,
"properties": {
"encryptedContactName": {
"type": "string"
},
"messageBatch": {
"items": {
"$ref": "#/definitions/MessageRecord"
},
"type": "array"
}
},
"required": [
"messageBatch",
"encryptedContactName"
],
"type": "object"
}
}
}
10 changes: 10 additions & 0 deletions packages/backend/src/schema/storage/AddMessageBatchRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AddMessageBatchRequestSchema from './AddMessageBatchRequest.schema.json';
import { MessageRecord } from '../../persistence/storage';

//This schema defines how the body of the addMessageBatch request has to look like
export interface _AddMessageBatchRequest {
messageBatch: MessageRecord[];
//The encrypted contact name
encryptedContactName: string;
}
export const AddMessageBatchRequest = AddMessageBatchRequestSchema;
30 changes: 30 additions & 0 deletions packages/backend/src/schema/storage/AddMessageRequest.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$ref": "#/definitions/_AddMessageRequest",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"_AddMessageRequest": {
"additionalProperties": false,
"properties": {
"createdAt": {
"type": "number"
},
"encryptedContactName": {
"type": "string"
},
"encryptedEnvelopContainer": {
"type": "string"
},
"messageId": {
"type": "string"
}
},
"required": [
"encryptedEnvelopContainer",
"encryptedContactName",
"messageId",
"createdAt"
],
"type": "object"
}
}
}
15 changes: 15 additions & 0 deletions packages/backend/src/schema/storage/AddMesssageRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AddMessageRequestSchema from './AddMessageRequest.schema.json';

//This schema defines how the body of the getMessage request has to look like
export interface _AddMessageRequest {
//The encrypted message container
encryptedEnvelopContainer: string;
//The encrypted contact name
encryptedContactName: string;
//The message id defined by the client
messageId: string;
//The time the message was created, also defined by the client
createdAt: number;
}
export const AddMessageRequest =
AddMessageRequestSchema.definitions._AddMessageRequest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$ref": "#/definitions/_EditMessageBatchRequest",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MessageRecord": {
"additionalProperties": false,
"properties": {
"createdAt": {
"type": "number"
},
"encryptedEnvelopContainer": {
"type": "string"
},
"messageId": {
"type": "string"
}
},
"required": [
"createdAt",
"messageId",
"encryptedEnvelopContainer"
],
"type": "object"
},
"_EditMessageBatchRequest": {
"additionalProperties": false,
"properties": {
"editMessageBatchPayload": {
"items": {
"$ref": "#/definitions/MessageRecord"
},
"type": "array"
},
"encryptedContactName": {
"type": "string"
}
},
"required": [
"encryptedContactName",
"editMessageBatchPayload"
],
"type": "object"
}
}
}
10 changes: 10 additions & 0 deletions packages/backend/src/schema/storage/EditMessageBatchRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import EditMessageBatchRequestSchema from './EditMessageBatchRequest.schema.json';
import { _AddMessageRequest } from './AddMesssageRequest';
import { MessageRecord } from '../../persistence/storage';

//This schema defines how the body of the editMessageBatch request has to look like
export interface _EditMessageBatchRequest {
encryptedContactName: string;
editMessageBatchPayload: MessageRecord[];
}
export const EditMessageBatchRequest = EditMessageBatchRequestSchema;
22 changes: 22 additions & 0 deletions packages/backend/src/schema/storage/PaginatedRequest.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$ref": "#/definitions/_PaginatedRequest",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"_PaginatedRequest": {
"additionalProperties": false,
"properties": {
"offset": {
"type": "number"
},
"pageSize": {
"type": "number"
}
},
"required": [
"pageSize",
"offset"
],
"type": "object"
}
}
}
23 changes: 23 additions & 0 deletions packages/backend/src/schema/storage/PaginatedRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import PaginatedRequestSchema from './PaginatedRequest.schema.json';

//This schema defines how the body of the GetMessages request has to look like
export interface _PaginatedRequest {
pageSize: number;
offset: number;
}
export const PaginatedRequest = {
...PaginatedRequestSchema,
definitions: {
...PaginatedRequestSchema.definitions,
_PaginatedRequest: {
...PaginatedRequestSchema.definitions._PaginatedRequest,
properties: {
...PaginatedRequestSchema.definitions._PaginatedRequest
.properties,
//Extending schema to prevent negative numbers
pageSize: { type: 'number', minimum: 0 },
offset: { type: 'number', minimum: 0 },
},
},
},
};
Loading

0 comments on commit d5eecf6

Please sign in to comment.