Skip to content

Commit

Permalink
feat: update seed, remove lorem ipsum elements on staging
Browse files Browse the repository at this point in the history
  • Loading branch information
wielopolski committed Dec 9, 2024
1 parent aa82717 commit f3ffef9
Show file tree
Hide file tree
Showing 15 changed files with 1,594 additions and 84 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-api-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
ECS_CLUSTER: ${{ vars.AWS_ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.AWS_ECS_SERVICE }}
DOCKER_IMAGE: ${{ secrets.AWS_ECR_REGISTRY }}:${{ github.sha }}
run: selleo aws ecs deploy --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --docker-image $DOCKER_IMAGE --one-off migrate --one-off seed
run: selleo aws ecs deploy --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --docker-image $DOCKER_IMAGE --one-off migrate --one-off seed-staging

- name: ECS Run migrations
env:
Expand All @@ -70,4 +70,4 @@ jobs:
AWS_REGION: ${{ vars.AWS_REGION }}
ECS_CLUSTER: ${{ vars.AWS_ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.AWS_ECS_SERVICE }}
run: selleo aws ecs run --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --one-off seed
run: selleo aws ecs run --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --one-off seed-staging
7 changes: 5 additions & 2 deletions apps/api/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ if [ $COMMAND = "server" ]; then
elif [ $COMMAND = "migrate" ]; then
echo "Running migrations..."
npm run db:migrate
elif [ $COMMAND = "seed" ]; then
elif [ $COMMAND = "seed-staging" ]; then
echo "Running seeds..."
npm run db:seed-staging
elif [ $COMMAND = "seed-prod" ]; then
echo "Running seeds..."
npm run db:seed-prod
elif [ $COMMAND = "truncate-tables" ]; then
echo "Truncating tables..."
npm run db:truncate-tables
else
echo "Usage: entrypoint.sh [server|migrate|seed|truncate-tables]"
echo "Usage: entrypoint.sh [server|migrate|seed-staging|seed-prod|truncate-tables]"
exit 1
fi
5 changes: 3 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"test:e2e:watch": "jest --config ./test/jest-e2e.json --watch",
"db:migrate": "drizzle-kit migrate",
"db:generate": "drizzle-kit generate",
"db:seed": "ts-node -r tsconfig-paths/register ./src/seed.ts",
"db:seed-prod": "node dist/src/seed.js",
"db:seed": "ts-node -r tsconfig-paths/register ./src/seed/seed.ts",
"db:seed-staging": "node dist/src/seed/seed-staging.js",
"db:seed-prod": "node dist/src/seed/seed.js",
"db:truncate-tables": "node dist/src/truncateTables.js"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/courses/courses.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class CoursesController {
};
const query = { filters, page, perPage, sort };

const data = await this.coursesService.getAvailableCourses(query);
const data = await this.coursesService.getAvailableCourses(query, currentUserId);

return new PaginatedResponse(data);
}
Expand Down
74 changes: 52 additions & 22 deletions apps/api/src/courses/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ilike,
inArray,
isNotNull,
isNull,
like,
sql,
} from "drizzle-orm";
Expand Down Expand Up @@ -85,14 +84,14 @@ export class CoursesService {

const { sortOrder, sortedField } = getSortOptions(sort);

return await this.db.transaction(async (tx) => {
return await this.db.transaction(async (trx) => {
const conditions = this.getFiltersConditions(filters, false);

if (currentUserRole === USER_ROLES.teacher && currentUserId) {
conditions.push(eq(courses.authorId, currentUserId));
}

const queryDB = tx
const queryDB = trx
.select({
id: courses.id,
description: sql<string>`${courses.description}`,
Expand Down Expand Up @@ -163,11 +162,11 @@ export class CoursesService {

const { sortOrder, sortedField } = getSortOptions(sort);

return this.db.transaction(async (tx) => {
return this.db.transaction(async (trx) => {
const conditions = [eq(studentCourses.studentId, userId)];
conditions.push(...this.getFiltersConditions(filters));

const queryDB = tx
const queryDB = trx
.select(this.getSelectField())
.from(studentCourses)
.innerJoin(courses, eq(studentCourses.courseId, courses.id))
Expand Down Expand Up @@ -196,7 +195,7 @@ export class CoursesService {
const dynamicQuery = queryDB.$dynamic();
const paginatedQuery = addPagination(dynamicQuery, page, perPage);
const data = await paginatedQuery;
const [{ totalItems }] = await tx
const [{ totalItems }] = await trx
.select({ totalItems: countDistinct(courses.id) })
.from(studentCourses)
.innerJoin(courses, eq(studentCourses.courseId, courses.id))
Expand All @@ -220,8 +219,10 @@ export class CoursesService {
});
}

// TODO: remove unused select fields
async getAvailableCourses(
query: CoursesQuery,
currentUserId: UUIDType,
): Promise<{ data: AllCoursesResponse; pagination: Pagination }> {
const {
sort = CourseSortFields.title,
Expand All @@ -231,18 +232,49 @@ export class CoursesService {
} = query;
const { sortOrder, sortedField } = getSortOptions(sort);

return this.db.transaction(async (tx) => {
const conditions = [
eq(courses.state, STATES.published),
eq(courses.archived, false),
isNull(studentCourses.studentId),
];
return this.db.transaction(async (trx) => {
const notEnrolledCourses: Record<string, string>[] = await trx.execute(sql`
SELECT id AS "courseId"
FROM courses
WHERE id NOT IN (
SELECT DISTINCT course_id
FROM student_courses
WHERE student_id = ${currentUserId}
)`);
const notEnrolledCourseIds = notEnrolledCourses.map(({ courseId }) => courseId);

const conditions = [eq(courses.state, STATES.published), eq(courses.archived, false)];
conditions.push(...this.getFiltersConditions(filters));

const queryDB = tx
.select(this.getSelectField())
if (notEnrolledCourses.length > 0) {
conditions.push(inArray(courses.id, notEnrolledCourseIds));
}

const queryDB = trx
.select({
id: courses.id,
description: sql<string>`${courses.description}`,
title: courses.title,
imageUrl: courses.imageUrl,
authorId: sql<string>`${courses.authorId}`,
author: sql<string>`CONCAT(${users.firstName} || ' ' || ${users.lastName})`,
authorEmail: sql<string>`${users.email}`,
category: sql<string>`${categories.title}`,
enrolled: sql<boolean>`FALSE`,
enrolledParticipantCount: sql<number>`COALESCE(${coursesSummaryStats.freePurchasedCount} + ${coursesSummaryStats.paidPurchasedCount}, 0)`,
courseLessonCount: courses.lessonsCount,
completedLessonCount: sql<number>`0`,
priceInCents: courses.priceInCents,
currency: courses.currency,
hasFreeLessons: sql<boolean>`
EXISTS (
SELECT 1
FROM ${courseLessons}
WHERE ${courseLessons.courseId} = ${courses.id}
AND ${courseLessons.isFree} = true
)`,
})
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
.leftJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.leftJoin(courseLessons, eq(courses.id, courseLessons.courseId))
Expand All @@ -257,18 +289,16 @@ export class CoursesService {
users.firstName,
users.lastName,
users.email,
studentCourses.studentId,
categories.title,
coursesSummaryStats.freePurchasedCount,
coursesSummaryStats.paidPurchasedCount,
studentCourses.finishedLessonsCount,
)
.orderBy(sortOrder(this.getColumnToSortBy(sortedField as CourseSortField)));

const dynamicQuery = queryDB.$dynamic();
const paginatedQuery = addPagination(dynamicQuery, page, perPage);
const data = await paginatedQuery;
const [{ totalItems }] = await tx
const [{ totalItems }] = await trx
.select({ totalItems: countDistinct(courses.id) })
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
Expand Down Expand Up @@ -643,8 +673,8 @@ export class CoursesService {
image?: Express.Multer.File,
currentUserId?: UUIDType,
) {
return this.db.transaction(async (tx) => {
const [existingCourse] = await tx.select().from(courses).where(eq(courses.id, id));
return this.db.transaction(async (trx) => {
const [existingCourse] = await trx.select().from(courses).where(eq(courses.id, id));

if (!existingCourse) {
throw new NotFoundException("Course not found");
Expand All @@ -655,7 +685,7 @@ export class CoursesService {
}

if (updateCourseBody.categoryId) {
const [category] = await tx
const [category] = await trx
.select()
.from(categories)
.where(eq(categories.id, updateCourseBody.categoryId));
Expand All @@ -681,7 +711,7 @@ export class CoursesService {
...(imageKey && { imageUrl: imageKey.fileUrl }),
};

const [updatedCourse] = await tx
const [updatedCourse] = await trx
.update(courses)
.set(updateData)
.where(eq(courses.id, id))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LESSON_ITEM_TYPE, LESSON_TYPE } from "./lessons/lesson.type";
import { STATUS } from "./storage/schema/utils";
import { LESSON_ITEM_TYPE, LESSON_TYPE } from "../lessons/lesson.type";
import { STATUS } from "../storage/schema/utils";

import type { NiceCourseData } from "./utils/types/test-types";
import type { NiceCourseData } from "../utils/types/test-types";

export const e2eCourses: NiceCourseData[] = [
{
Expand Down
Loading

0 comments on commit f3ffef9

Please sign in to comment.