Skip to content

Commit

Permalink
Merge pull request #918 from parlemonde/revert-917-ft/VIL-358/relatio…
Browse files Browse the repository at this point in the history
…n-user-country-b

Revert "Ft/vil 358/relation user country b"
  • Loading branch information
DavidRobertAnsart authored May 16, 2024
2 parents a86ea30 + a544696 commit 53940c9
Show file tree
Hide file tree
Showing 69 changed files with 439 additions and 565 deletions.
12 changes: 5 additions & 7 deletions server/__tests__/fileUpload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs';
import path from 'path';

import { uploadFiles } from '../controllers/filesController';
import type { User } from '../entities/user';
import { AwsS3 } from '../fileUpload/s3';
import { AppError } from '../middlewares/handleErrors';
import { appDataSource, fakeUser } from './mock';
Expand All @@ -12,9 +13,6 @@ beforeAll(() => {
});
beforeEach(() => {});
afterAll(() => {
const directory = path.join(__dirname, '../fileUpload/images/1/');
// cleqning dev images files
fs.rmSync(directory, { recursive: true, force: true });
return appDataSource.destroy();
});

Expand All @@ -29,7 +27,7 @@ describe('Upload files', () => {
});

const mockRequest: Partial<Request> = {
user: fakeUser,
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
Expand All @@ -54,7 +52,7 @@ describe('Upload files', () => {
});
test('Should throw - files are missing -', async () => {
const mockRequest: Partial<Request> = {
user: fakeUser,
user: fakeUser as User,
files: [],
};

Expand Down Expand Up @@ -85,7 +83,7 @@ describe('Upload files', () => {
});

const mockRequest: Partial<Request> = {
user: fakeUser,
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
Expand Down Expand Up @@ -114,7 +112,7 @@ describe('Upload files', () => {
});

const mockRequest: Partial<Request> = {
user: fakeUser,
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
Expand Down
5 changes: 2 additions & 3 deletions server/__tests__/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import supertest from 'supertest';
import { DataSource } from 'typeorm';

import { getApp } from '../app';
import type { User } from '../entities/user';

export const appDataSource = new DataSource({
type: 'sqlite',
Expand Down Expand Up @@ -54,6 +53,6 @@ export const fakeUser = {
firstLogin: 3,
type: 0,
villageId: 1,
country: { id: 0, isoCode: 'FR', name: 'France' },
country: { isoCode: 'FR', name: 'France' },
position: { lat: 48.8863442, lng: 2.380321 },
} as unknown as User;
};
2 changes: 1 addition & 1 deletion server/app-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import morgan from 'morgan';
import next from 'next';
import path from 'path';

import { UserType } from '../types/user.type';
import { UserType } from './entities/user';
import { handleErrors } from './middlewares/handleErrors';
import { removeTrailingSlash } from './middlewares/trailingSlash';

Expand Down
2 changes: 1 addition & 1 deletion server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import morgan from 'morgan';
import next from 'next';
import path from 'path';

import { UserType } from '../types/user.type';
import { authRouter } from './authentication';
import { controllerRouter } from './controllers';
import { UserType } from './entities/user';
import { getH5pRouter } from './h5p';
import { authenticate } from './middlewares/authenticate';
import { crsfProtection } from './middlewares/csrfCheck';
Expand Down
3 changes: 1 addition & 2 deletions server/authentication/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { JSONSchemaType } from 'ajv';
import * as argon2 from 'argon2';
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { User } from '../entities/user';
import { User, UserType } from '../entities/user';
import { UserToStudent } from '../entities/userToStudent';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { AppDataSource } from '../utils/data-source';
Expand Down
12 changes: 9 additions & 3 deletions server/controllers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { EPhase1Steps, ActivityStatus, ActivityType, EPhase2Steps, EPhase3Steps
import type { GameData, GamesData } from '../../types/game.type';
import type { StoriesData, StoryElement } from '../../types/story.type';
import { ImageType } from '../../types/story.type';
import { UserType } from '../../types/user.type';
import { VillagePhase } from '../../types/village.type';
import { Activity } from '../entities/activity';
import { Game } from '../entities/game';
import { Image } from '../entities/image';
import { UserType } from '../entities/user';
import { VillagePhase } from '../entities/village';
import { getActivities, getActivitiesCommentCount } from '../manager/activity';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { getQueryString } from '../utils';
Expand All @@ -25,11 +25,17 @@ const activityController = new Controller('/activities');
// --- Get all activities. ---
activityController.get({ path: '', userType: UserType.OBSERVATOR }, async (req: Request, res: Response) => {
if (!req.user) throw new AppError('Forbidden', ErrorCode.UNKNOWN);

const activities = await getActivities({
limit: req.query.limit ? Number(getQueryString(req.query.limit)) || 200 : undefined,
page: req.query.page ? Number(getQueryString(req.query.page)) || 0 : undefined,
villageId: req.query.villageId ? Number(getQueryString(req.query.villageId)) || 0 : undefined,
countries: req.query.countries as string[] | undefined,
countries:
req.query.countries !== undefined
? req.query.countries.length === 0
? []
: (getQueryString(req.query.countries) || '').split(',')
: undefined,
pelico: req.query.pelico ? req.query.pelico !== 'false' : undefined,
type: req.query.type ? (getQueryString(req.query.type) || '').split(',') : undefined,
subType: req.query.subType ? Number(getQueryString(req.query.subType)) || 0 : undefined,
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/analytic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import useragent from 'express-useragent';
import { Between } from 'typeorm';

import type { AnalyticData, NavigationPerf, BrowserPerf } from '../../types/analytics.type';
import { UserType } from '../../types/user.type';
import { AnalyticSession, AnalyticPageView, AnalyticPerformance } from '../entities/analytic';
import { UserType } from '../entities/user';
import { AppError, ErrorCode, handleErrors } from '../middlewares/handleErrors';
import { generateTemporaryToken, getQueryString } from '../utils';
import { AppDataSource } from '../utils/data-source';
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/archive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request, Response, NextFunction } from 'express';

import { UserType } from '../../types/user.type';
import { UserType } from '../entities/user';
import { streamFile } from '../fileUpload/streamFile';
import { Controller } from './controller';

Expand Down
2 changes: 1 addition & 1 deletion server/controllers/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';

import { UserType } from '../../types/user.type';
import { UserType } from '../entities/user';
import { deleteFile, uploadFile } from '../fileUpload';
import { streamFile } from '../fileUpload/streamFile';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
Expand Down
16 changes: 6 additions & 10 deletions server/controllers/classroom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { JSONSchemaType } from 'ajv';
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { Classroom } from '../entities/classroom';
import { Country } from '../entities/country';
import { User } from '../entities/user';
import { User, UserType } from '../entities/user';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { AppDataSource } from '../utils/data-source';
import { ajv, sendInvalidDataError } from '../utils/jsonSchemaValidator';
Expand All @@ -26,7 +24,6 @@ classroomController.get({ path: '/:id', userType: UserType.TEACHER }, async (req
const classroom = await AppDataSource.getRepository(Classroom).findOne({
relations: {
user: true,
country: true,
},
where: { user: { id } },
});
Expand Down Expand Up @@ -88,13 +85,12 @@ classroomController.post({ path: '', userType: UserType.TEACHER }, async (req: R
where: { user: { id: req.user.id } },
});
if (verification.length !== 0) return res.status(303).send('Classroom already exit');
const countryFound = await AppDataSource.getRepository(Country).findOne({ where: { isoCode: data.countryCode } });
const classroom = new Classroom();
classroom.user.id = data.userId;
classroom.country = countryFound;
classroom.id = data.villageId;

await AppDataSource.getRepository(Classroom).save(classroom);
const classroom = await AppDataSource.createQueryBuilder()
.insert()
.into(Classroom)
.values([{ user: { id: data.userId }, village: { id: data.villageId }, countryCode: data.countryCode }])
.execute();

res.json(classroom);
});
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/comment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { JSONSchemaType } from 'ajv';
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { Activity } from '../entities/activity';
import { Comment } from '../entities/comment';
import { UserType } from '../entities/user';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { AppDataSource } from '../utils/data-source';
import { ajv, sendInvalidDataError } from '../utils/jsonSchemaValidator';
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs-extra';
import multer from 'multer';
import path from 'path';

import type { UserType } from '../../types/user.type';
import type { UserType } from '../entities/user';
import { authenticate } from '../middlewares/authenticate';
import { handleErrors } from '../middlewares/handleErrors';
import { diskStorage } from '../middlewares/multer';
Expand Down
6 changes: 2 additions & 4 deletions server/controllers/countries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { UserType } from '../../types/user.type';
import { Country } from '../entities/country';
import { AppDataSource } from '../utils/data-source';
import { UserType } from '../entities/user';
import { countries } from '../utils/iso-3166-countries-french';
import { Controller } from './controller';

const countryController = new Controller('/countries');

//--- Get all countries ---
countryController.get({ path: '', userType: UserType.TEACHER }, async (_req, res) => {
const countries = await AppDataSource.getRepository(Country).find();
res.sendJSON(countries);
});

Expand Down
2 changes: 1 addition & 1 deletion server/controllers/currencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserType } from '../../types/user.type';
import { UserType } from '../entities/user';
import { currencies } from '../utils/iso-4217-currencies-french';
import { Controller } from './controller';

Expand Down
3 changes: 1 addition & 2 deletions server/controllers/featureFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { NextFunction, Request, Response } from 'express';
import { In } from 'typeorm';

import type { FeatureFlagsNames } from '../../types/featureFlag.constant';
import { UserType } from '../../types/user.type';
import { FeatureFlag } from '../entities/featureFlag';
import { User } from '../entities/user';
import { User, UserType } from '../entities/user';
import { AppDataSource } from '../utils/data-source';
import { Controller } from './controller';

Expand Down
2 changes: 1 addition & 1 deletion server/controllers/game.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { JSONSchemaType } from 'ajv';
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { Game } from '../entities/game';
import { GameResponse } from '../entities/gameResponse';
import { UserType } from '../entities/user';
import { getQueryString } from '../utils';
import { AppDataSource } from '../utils/data-source';
import { ajv, sendInvalidDataError } from '../utils/jsonSchemaValidator';
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import sharp from 'sharp';
import { v4 as uuidv4 } from 'uuid';

import { UserType } from '../../types/user.type';
import { UserType } from '../entities/user';
import { deleteFile, uploadFile } from '../fileUpload';
import { streamFile } from '../fileUpload/streamFile';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/story.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Request, Response, NextFunction } from 'express';

import { ImageType } from '../../types/story.type';
import { UserType } from '../../types/user.type';
import { Image } from '../entities/image';
import { UserType } from '../entities/user';
import { getQueryString } from '../utils';
import { AppDataSource } from '../utils/data-source';
import { Controller } from './controller';
Expand Down
11 changes: 3 additions & 8 deletions server/controllers/student.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { JSONSchemaType } from 'ajv';
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { Classroom } from '../entities/classroom';
import { Student } from '../entities/student';
import { User } from '../entities/user';
import { User, UserType } from '../entities/user';
import { UserToStudent } from '../entities/userToStudent';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { getQueryString } from '../utils';
Expand Down Expand Up @@ -120,10 +118,7 @@ studentController.post({ path: '', userType: UserType.TEACHER }, async (req: Req
throw new AppError('Forbidden', ErrorCode.UNKNOWN);
}
const student = new Student();
const classroomFound = await AppDataSource.getRepository(Classroom).findOne({ where: { id: data.classroomId } });
if (classroomFound) {
student.classroom = classroomFound;
}
student.classroom = data.classroomId;
student.firstname = data.firstname ?? null;
student.lastname = data.lastname ?? null;
student.hashedCode = inviteCodeGenerator(10);
Expand Down Expand Up @@ -181,7 +176,7 @@ studentController.post({ path: '/link-student', userType: UserType.FAMILY }, asy
villageId: student.classroom.villageId,
hasStudentLinked: true,
firstLogin: student.classroom.village?.activePhase, //TEST THIS LINE
country: student.classroom.country,
countryCode: student.classroom.countryCode as string,
})
.where('id = :id', { id: req.user.id })
.execute();
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/teacher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextFunction, Request, Response } from 'express';

import { UserType } from '../../types/user.type';
import { Activity } from '../entities/activity';
import { UserType } from '../entities/user';
import { UserToStudent } from '../entities/userToStudent';
import { AppError, ErrorCode } from '../middlewares/handleErrors';
import { AppDataSource } from '../utils/data-source';
Expand Down
Loading

0 comments on commit 53940c9

Please sign in to comment.