430 lines
15 KiB
JavaScript
430 lines
15 KiB
JavaScript
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const { STATUS_CODES } = require('node:http');
|
||
|
||
const directory = path.join(__dirname, '..', 'postman');
|
||
const { examples } = JSON.parse(
|
||
fs.readFileSync(path.join(directory, 'music-world-response-examples.json'), 'utf8'),
|
||
);
|
||
const body = (data) => ({
|
||
mode: 'raw',
|
||
raw: JSON.stringify(data, null, 2).replace('"{{progressRevision}}"', '{{progressRevision}}'),
|
||
options: { raw: { language: 'json' } },
|
||
});
|
||
const bearer = (value) => ({ type: 'bearer', bearer: [{ key: 'token', value, type: 'string' }] });
|
||
const local = (variable) => '{{' + variable + '}}';
|
||
|
||
function item(name, method, endpoint, options = {}) {
|
||
const url = options.absolute ? endpoint : '{{baseUrl}}' + endpoint;
|
||
const request = {
|
||
method,
|
||
url,
|
||
description: options.description,
|
||
header: [
|
||
...(options.data ? [{ key: 'Content-Type', value: 'application/json' }] : []),
|
||
...(options.key ? [{ key: 'Idempotency-Key', value: local(options.key) }] : []),
|
||
...(options.headers ?? []),
|
||
],
|
||
auth: options.admin
|
||
? bearer('{{superadminAccessToken}}')
|
||
: options.public
|
||
? { type: 'noauth' }
|
||
: undefined,
|
||
...(options.data ? { body: body(options.data) } : {}),
|
||
};
|
||
const capture = examples.find((entry) => entry.name === (options.example ?? name));
|
||
const response = capture
|
||
? [
|
||
{
|
||
name: capture.name,
|
||
originalRequest: {
|
||
...request,
|
||
url: '{{baseUrl}}' + capture.path,
|
||
...(capture.request ? { body: body(capture.request) } : {}),
|
||
},
|
||
status: STATUS_CODES[capture.status],
|
||
code: capture.status,
|
||
_postman_previewlanguage: 'json',
|
||
header: [{ key: 'Content-Type', value: 'application/json' }],
|
||
body: JSON.stringify(capture.response, null, 2),
|
||
},
|
||
]
|
||
: [];
|
||
const statuses = options.statuses ?? [method === 'POST' ? 201 : 200];
|
||
const commands = [
|
||
'pm.test("Expected HTTP status", () => pm.expect(' +
|
||
JSON.stringify(statuses) +
|
||
').to.include(pm.response.code));',
|
||
];
|
||
if (options.variables) {
|
||
commands.push(
|
||
'if (pm.response.code >= 200 && pm.response.code < 300) {',
|
||
' const data = pm.response.json();',
|
||
);
|
||
for (const [key, expression] of Object.entries(options.variables)) {
|
||
commands.push(
|
||
' if (' +
|
||
expression +
|
||
' !== undefined && ' +
|
||
expression +
|
||
' !== null) pm.collectionVariables.set(' +
|
||
JSON.stringify(key) +
|
||
', String(' +
|
||
expression +
|
||
'));',
|
||
);
|
||
}
|
||
commands.push('}');
|
||
}
|
||
return {
|
||
name,
|
||
request,
|
||
response,
|
||
event: [{ listen: 'test', script: { type: 'text/javascript', exec: commands } }],
|
||
};
|
||
}
|
||
|
||
const folder = (name, item) => ({ name, item });
|
||
const get = (name, url, options) => item(name, 'GET', url, options);
|
||
const post = (name, url, data, options) => item(name, 'POST', url, { ...options, data });
|
||
const adminPrefix = '/superadmin/music-world';
|
||
const adminPost = (name, url, data, options) =>
|
||
post(name, adminPrefix + url, data, { ...options, admin: true });
|
||
const quote = (name, url, data, quoteVariable) =>
|
||
post(name, url + '/preview', data, {
|
||
statuses: [200],
|
||
variables: { [quoteVariable]: 'data.quoteId' },
|
||
});
|
||
const reserve = (name, url, quoteVariable, key, bookingVariable, options = {}) =>
|
||
post(
|
||
name,
|
||
url,
|
||
{ quoteId: local(quoteVariable), notes: 'Flutter test' },
|
||
{
|
||
key,
|
||
...options,
|
||
variables: {
|
||
[bookingVariable]: 'data.id',
|
||
paymentBookingId: 'data.id',
|
||
paymentKind: 'data.kind',
|
||
},
|
||
},
|
||
);
|
||
const booleanSave = (name, path) => [
|
||
item('Save ' + name, 'PUT', path + '/save'),
|
||
item('Unsave ' + name, 'DELETE', path + '/save'),
|
||
];
|
||
|
||
const discover = '/discovery/search';
|
||
const event = '/events/{{eventId}}';
|
||
const eventBooking = event + '/bookings';
|
||
const online = '/courses/{{courseId}}';
|
||
const purchases = online + '/purchases';
|
||
const centerCourse = '/courses/{{inPersonCourseId}}';
|
||
const seatBooking = centerCourse + '/bookings';
|
||
const lesson = online + '/lessons/{{lessonId}}';
|
||
const dates = { startsAt: '{{startsAt}}', endsAt: '{{endsAt}}', timeZone: 'Asia/Riyadh' };
|
||
const sale = { salesStartAt: '{{salesStartAt}}', salesEndAt: '{{salesEndAt}}' };
|
||
const catalog = { category: 'Oud', currency: 'SAR', instructorId: '{{instructorId}}' };
|
||
const place = { location: 'Riyadh', latitude: 24.71, longitude: 46.67 };
|
||
|
||
const collection = {
|
||
info: {
|
||
name: 'Oudelaa Music World – Flutter',
|
||
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
|
||
description:
|
||
'Run authenticated requests with a mobile JWT. For empty test databases, run Catalog setup with a separate superadmin JWT and a synthetic linked user. See MUSIC-WORLD-FLUTTER.md for workflow, environments and statuses.',
|
||
},
|
||
auth: bearer('{{accessToken}}'),
|
||
variable: [
|
||
{ key: 'baseUrl', value: 'http://localhost:4000/api/v1' },
|
||
{ key: 'accessToken', value: '' },
|
||
{ key: 'superadminAccessToken', value: '' },
|
||
{ key: 'linkedUserId', value: '' },
|
||
{ key: 'searchQuery', value: 'Oud' },
|
||
{ key: 'progressRevision', value: '0' },
|
||
],
|
||
event: [
|
||
{
|
||
listen: 'prerequest',
|
||
script: {
|
||
type: 'text/javascript',
|
||
exec: [
|
||
'const now = Date.now();',
|
||
'const at = (h) => new Date(now + h * 3600000).toISOString();',
|
||
'for (const [key, hours] of Object.entries({ startsAt: 96, endsAt: 100, salesStartAt: -5, salesEndAt: 90, couponExpiresAt: 24 })) {',
|
||
' pm.collectionVariables.set(key, at(hours));',
|
||
'}',
|
||
'for (const key of ["eventBookingKey", "coursePurchaseKey", "seatBookingKey", "paymentKey"]) {',
|
||
' if (!pm.collectionVariables.get(key)) pm.collectionVariables.set(key, pm.variables.replaceIn("{{$guid}}"));',
|
||
'}',
|
||
],
|
||
},
|
||
},
|
||
],
|
||
item: [
|
||
folder('Discover', [
|
||
get(
|
||
'Discover talents',
|
||
'/users/discover?q={{searchQuery}}&hasAvatarOnly=false&includeRoleBuckets=true&page=1&limit=8',
|
||
{ example: 'Talent counters and role buckets' },
|
||
),
|
||
get('Discovery features by app version', '/discovery/features?appVersion=1.2.0', {
|
||
public: true,
|
||
}),
|
||
get('All discovery groups', discover + '?q={{searchQuery}}&type=all&page=1&limit=10'),
|
||
get(
|
||
'Unified search with independent pages',
|
||
discover +
|
||
'?q={{searchQuery}}&type=users,posts,events,courses&pages=users:2,events:1&limit=10&appVersion=1.2.0',
|
||
),
|
||
get('Empty results', discover + '?q=zzzznoresults&type=events'),
|
||
get('Unsupported discovery result type', discover + '?q={{searchQuery}}&type=unknown', {
|
||
statuses: [400],
|
||
}),
|
||
get('Existing global search', '/search?q={{searchQuery}}&type=all&page=1&limit=10'),
|
||
]),
|
||
folder('Events and tickets', [
|
||
get('List events', '/events?q={{searchQuery}}&page=1&limit=20'),
|
||
get('Upcoming events', '/events/upcoming?page=1&limit=20'),
|
||
get(
|
||
'Nearby events',
|
||
'/events/nearby?latitude=24.71&longitude=46.67&radiusKm=30&page=1&limit=20',
|
||
),
|
||
get('Event categories', '/events/categories'),
|
||
get('Event details', event),
|
||
get('Ticket categories', event + '/ticket-categories'),
|
||
...booleanSave('event', event),
|
||
get('Saved events', '/events/saved?page=1&limit=20'),
|
||
quote(
|
||
'Event booking preview',
|
||
eventBooking,
|
||
{ items: [{ ticketCategoryId: '{{ticketCategoryId}}', quantity: 1 }] },
|
||
'eventQuoteId',
|
||
),
|
||
reserve(
|
||
'Reserve event ticket',
|
||
eventBooking,
|
||
'eventQuoteId',
|
||
'eventBookingKey',
|
||
'eventBookingId',
|
||
),
|
||
get('Issued event ticket', eventBooking + '/{{eventBookingId}}', {
|
||
variables: { verificationCode: 'data.tickets[0]?.verificationCode' },
|
||
}),
|
||
get('My event bookings', '/events/bookings?page=1&limit=20'),
|
||
item('Cancel event booking', 'DELETE', eventBooking + '/{{eventBookingId}}'),
|
||
]),
|
||
folder('Courses and directories', [
|
||
get('List courses', '/courses?q={{searchQuery}}&mode=online&page=1&limit=20'),
|
||
get('Course categories', '/courses/categories'),
|
||
get('Online course details', online),
|
||
get('In-person course details', centerCourse),
|
||
get('Scheduled course offerings', centerCourse + '/offerings'),
|
||
...booleanSave('course', online),
|
||
get('Saved courses', '/courses/saved'),
|
||
get('Enrolled courses', '/courses/mine'),
|
||
get('List instructors', '/instructors?q={{searchQuery}}'),
|
||
get('Instructor details', '/instructors/{{instructorId}}'),
|
||
get('Instructor courses', '/instructors/{{instructorId}}/courses'),
|
||
get('List institutes', '/institutes?q={{searchQuery}}'),
|
||
get('Institute details', '/institutes/{{instituteId}}'),
|
||
get('Institute courses', '/institutes/{{instituteId}}/courses'),
|
||
get('Collaboration opportunities', '/opportunities?q={{searchQuery}}'),
|
||
]),
|
||
folder('Course transactions and learning', [
|
||
quote('Discounted course preview', purchases, { couponCode: 'TEST20' }, 'onlineQuoteId'),
|
||
reserve(
|
||
'Purchase online course',
|
||
purchases,
|
||
'onlineQuoteId',
|
||
'coursePurchaseKey',
|
||
'coursePurchaseId',
|
||
),
|
||
get('Confirmed online course purchase', purchases + '/{{coursePurchaseId}}'),
|
||
get('My course purchases', '/courses/purchases'),
|
||
item('Cancel online purchase', 'DELETE', purchases + '/{{coursePurchaseId}}'),
|
||
quote(
|
||
'In-person booking preview',
|
||
seatBooking,
|
||
{ offeringId: '{{offeringId}}', quantity: 1 },
|
||
'seatQuoteId',
|
||
),
|
||
reserve(
|
||
'Reserve scheduled seat',
|
||
seatBooking,
|
||
'seatQuoteId',
|
||
'seatBookingKey',
|
||
'seatBookingId',
|
||
),
|
||
get('Confirmed in-person booking', seatBooking + '/{{seatBookingId}}'),
|
||
get('My in-person bookings', '/courses/bookings'),
|
||
item('Cancel seat reservation', 'DELETE', seatBooking + '/{{seatBookingId}}'),
|
||
get('Check course access', online + '/access'),
|
||
get('Authorized lessons', online + '/lessons', {
|
||
variables: { lessonId: 'data.items[0]?.id' },
|
||
}),
|
||
get('Temporary lesson video', lesson + '/playback', {
|
||
variables: { lessonVideoUrl: '(data.requiresAuthorization ? data.url : "")' },
|
||
}),
|
||
get('Local video range example', '{{lessonVideoUrl}}', {
|
||
absolute: true,
|
||
statuses: [206],
|
||
headers: [{ key: 'Range', value: 'bytes=0-15' }],
|
||
}),
|
||
get('Course progress', online + '/progress', {
|
||
variables: { progressRevision: 'data.items[0]?.revision' },
|
||
}),
|
||
item('Update lesson progress', 'PUT', lesson + '/progress', {
|
||
data: { positionSeconds: 90, revision: '{{progressRevision}}' },
|
||
}),
|
||
get('My review and eligibility', online + '/review'),
|
||
item('Student review', 'PUT', online + '/review', {
|
||
data: { courseRating: 5, instructorRating: 4, comment: 'Excellent' },
|
||
}),
|
||
]),
|
||
folder('Shared payments', [
|
||
get(
|
||
'Available methods',
|
||
'/payments/methods?kind={{paymentKind}}&bookingId={{paymentBookingId}}',
|
||
{
|
||
description:
|
||
'Set paymentKind to event, online_course or in_person_course, and use the matching booking or purchase ID.',
|
||
},
|
||
),
|
||
post(
|
||
'Start sandbox payment',
|
||
'/payments',
|
||
{ kind: '{{paymentKind}}', bookingId: '{{paymentBookingId}}', method: 'sandbox' },
|
||
{ key: 'paymentKey', variables: { paymentId: 'data.id' } },
|
||
),
|
||
post(
|
||
'Start pay at center',
|
||
'/payments',
|
||
{ kind: 'in_person_course', bookingId: '{{seatBookingId}}', method: 'pay_at_center' },
|
||
{ key: 'paymentKey', variables: { paymentId: 'data.id' } },
|
||
),
|
||
get('Payment status', '/payments/{{paymentId}}'),
|
||
post(
|
||
'Confirm sandbox payment',
|
||
'/payments/{{paymentId}}/simulate',
|
||
{ outcome: 'succeeded' },
|
||
{ statuses: [200] },
|
||
),
|
||
post(
|
||
'Fail sandbox payment',
|
||
'/payments/{{paymentId}}/simulate',
|
||
{ outcome: 'failed' },
|
||
{ statuses: [200] },
|
||
),
|
||
post(
|
||
'Cancel sandbox payment',
|
||
'/payments/{{paymentId}}/simulate',
|
||
{ outcome: 'cancelled' },
|
||
{ statuses: [200] },
|
||
),
|
||
]),
|
||
folder('Catalog setup (superadmin)', [
|
||
adminPost(
|
||
'Create linked instructor',
|
||
'/instructors',
|
||
{ name: 'Oud Teacher', yearsExperience: 5, linkedUserId: '{{linkedUserId}}' },
|
||
{ variables: { instructorId: 'data.id' } },
|
||
),
|
||
adminPost(
|
||
'Institute with location',
|
||
'/institutes',
|
||
{ title: 'Oud Institute', place },
|
||
{ variables: { instituteId: 'data.id' } },
|
||
),
|
||
adminPost(
|
||
'Create event',
|
||
'/events',
|
||
{
|
||
title: 'Oud Concert',
|
||
venueName: 'Test Hall',
|
||
place,
|
||
...dates,
|
||
category: 'Oud',
|
||
currency: 'SAR',
|
||
ticketCategories: [
|
||
{ name: 'Standard', priceMinor: 900, capacity: 10, maxPerBooking: 2, ...sale },
|
||
],
|
||
},
|
||
{ variables: { eventId: 'data.id', ticketCategoryId: 'data.ticketCategories[0]?.id' } },
|
||
),
|
||
adminPost(
|
||
'Create online course',
|
||
'/courses',
|
||
{
|
||
...catalog,
|
||
title: 'Oud lessons',
|
||
mode: 'online',
|
||
priceMinor: 1200,
|
||
lessons: [
|
||
{ title: 'Oud technique', durationSeconds: 100, videoKey: 'private-lessons/oud.mp4' },
|
||
],
|
||
},
|
||
{ variables: { courseId: 'data.id' } },
|
||
),
|
||
adminPost(
|
||
'Create in-person course',
|
||
'/courses',
|
||
{
|
||
...catalog,
|
||
title: 'Oud class',
|
||
mode: 'in_person',
|
||
priceMinor: 2500,
|
||
instituteId: '{{instituteId}}',
|
||
},
|
||
{ variables: { inPersonCourseId: 'data.id' } },
|
||
),
|
||
adminPost(
|
||
'Create scheduled offering',
|
||
centerCourse + '/offerings',
|
||
{
|
||
instituteId: '{{instituteId}}',
|
||
...dates,
|
||
...sale,
|
||
capacity: 10,
|
||
maxPerBooking: 1,
|
||
priceMinor: 2500,
|
||
schedule: ['Saturday 10:00–12:00'],
|
||
payAtCenter: true,
|
||
},
|
||
{ variables: { offeringId: 'data.id' } },
|
||
),
|
||
adminPost('Create coupon', online + '/coupons', {
|
||
code: 'TEST20',
|
||
percent: 20,
|
||
expiresAt: '{{couponExpiresAt}}',
|
||
}),
|
||
adminPost('Create opportunity', '/opportunities', {
|
||
title: 'Oud ensemble rehearsal',
|
||
category: 'Oud',
|
||
linkedUserId: '{{linkedUserId}}',
|
||
closesAt: '{{couponExpiresAt}}',
|
||
}),
|
||
item('Change event status', 'PATCH', adminPrefix + event + '/status', {
|
||
admin: true,
|
||
data: { status: 'cancelled' },
|
||
}),
|
||
item('Change course status', 'PATCH', adminPrefix + online + '/status', {
|
||
admin: true,
|
||
data: { status: 'archived' },
|
||
}),
|
||
get('Verify ticket code', adminPrefix + '/tickets/{{verificationCode}}/verify', {
|
||
admin: true,
|
||
}),
|
||
adminPost('Verify payment at center', '/payments/{{paymentId}}/verify-at-center', undefined, {
|
||
statuses: [200],
|
||
}),
|
||
]),
|
||
],
|
||
};
|
||
|
||
fs.writeFileSync(
|
||
path.join(directory, 'Oudelaa-Music-World-Flutter.postman_collection.json'),
|
||
JSON.stringify(collection, null, 2) + '\n',
|
||
);
|